Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved} WHERE clause Qstring
Forum Updated to NodeBB v4.3 + New Features

[Solved} WHERE clause Qstring

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    andrewhopps
    wrote on last edited by
    #1

    Table layout example:

    @[product][quantity1][ piece1][quantity2][ piece2]
    [003893][ 1 ][003852][ 2 ][003845 ]@

    Query attempt:

    @ query->prepare("SELECT quantity1 FROM prints WHERE product = ?");
    query->bindValue(0, selectedPrint);@

    Query Result:

    @QSqlQuery::value: not positioned on a valid record@

    I am trying to return the number from the quantity1 column, using a variable.

    My variable is the text string of my currently selected list view item:
    @QString selectedPrint = ui->listView->model()->data(index).toString();@

    How do I use a string variable in my query?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Saugglocke
      wrote on last edited by
      #2

      Hey,

      does it work without the prepare?

      If not, then please show how you retrieve the records.

      bb

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        Hi,

        Did you run QSqlQuery::exec() and QSqlQuery::next() before QSqlQuery::value() ?

        See "the documentation":http://qt-project.org/doc/qt-5/qsqlquery.html for examples.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andrewhopps
          wrote on last edited by
          #4

          Retrieval to populate product list:
          @ QSqlQueryModel* modal=new QSqlQueryModel();
          QSqlQuery* query=new QSqlQuery(db);

              query->prepare("SELECT product FROM prints");
          
              query->exec();
              modal->setQuery(*query);
              ui->listView->setModel(modal);@
          

          Entire ListView that is main part of form:
          @void printdb::on_listView_clicked(const QModelIndex &index)
          {
          QString selectedPrint = ui->listView->model()->data(index).toString();
          QString direct = "prints/" % selectedPrint % ".jpg";
          QPixmap g(direct);

          scene = new QGraphicsScene(this);
          ui->graphicsView->setScene(scene);
          ui->graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
          scene->addPixmap(g);
          
          QString number = QString("%1").arg(selectedPrint.toInt(), 2, 10, QChar('0'));
          QSqlQuery* query=new QSqlQuery(db);
          query->prepare("SELECT quantity1 FROM prints WHERE product = ?");
          query->bindValue(0, selectedPrint);
          if(query->exec())
          {
              ui->lineEdit_2->setText(query->value(0).toString());
          }
          else
              qDebug() << query->lastError();@
          

          Running without prepare statement I get
          @QSqlError("", "Parameter count mismatch", "")@

          1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            You need to call QSqlQuery::next() before QSqlQuery::value().


            By the way, you don't have to create QSqlQuery on the heap.
            @
            // Replace this...
            QSqlQuery* query=new QSqlQuery(db);

            // ...with this...
            QSqlQuery query(db);
            @

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved