Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. QSqlQuery select statment
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery select statment

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
15 Posts 3 Posters 3.9k Views
  • 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.
  • M Offline
    M Offline
    MrLibya
    wrote on 13 Oct 2016, 10:17 last edited by MrLibya
    #1

    hallo all
    i will let the code talk

            QSqlQuery query;
            query.prepare("SELECT * FROM [main].[items] WHERE barcode = ? OR name = ? ");
            query.bindValue(0,ui->Numberinput->text());
            query.bindValue(1,ui->search_item->text());
            query.exec();
            query.first();
            ui->Cart->setItem(ui->Cart->currentRow(), ui->Cart->currentColumn(), new QTableWidgetItem(query.value(1).toString()));
    

    the error i get

    QSqlQuery::value: not positioned on a valid record
    
    R 1 Reply Last reply 13 Oct 2016, 10:22
    0
    • M MrLibya
      13 Oct 2016, 10:17

      hallo all
      i will let the code talk

              QSqlQuery query;
              query.prepare("SELECT * FROM [main].[items] WHERE barcode = ? OR name = ? ");
              query.bindValue(0,ui->Numberinput->text());
              query.bindValue(1,ui->search_item->text());
              query.exec();
              query.first();
              ui->Cart->setItem(ui->Cart->currentRow(), ui->Cart->currentColumn(), new QTableWidgetItem(query.value(1).toString()));
      

      the error i get

      QSqlQuery::value: not positioned on a valid record
      
      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 13 Oct 2016, 10:22 last edited by raven-worx
      #2

      @MrLibya
      are you sure that the query returns a valid result (QSqlQuery::isValid())?
      Maybe you could iterate over the result and check it.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      M 1 Reply Last reply 13 Oct 2016, 11:07
      2
      • R raven-worx
        13 Oct 2016, 10:22

        @MrLibya
        are you sure that the query returns a valid result (QSqlQuery::isValid())?
        Maybe you could iterate over the result and check it.

        M Offline
        M Offline
        MrLibya
        wrote on 13 Oct 2016, 11:07 last edited by
        #3

        @raven-worx yes query.isValid() return false , will we don't really need that ! , i'm already know the query is not Valid , but how to write the correct syntax for this query

        R 1 Reply Last reply 13 Oct 2016, 11:14
        0
        • M MrLibya
          13 Oct 2016, 11:07

          @raven-worx yes query.isValid() return false , will we don't really need that ! , i'm already know the query is not Valid , but how to write the correct syntax for this query

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 13 Oct 2016, 11:14 last edited by
          #4

          @MrLibya

          qDebug() << query.lastError().text()
          

          should give more hints whats the problem.

          But i guess the problem is the table name ([main].[items]), which isn't valid?!?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          M 1 Reply Last reply 13 Oct 2016, 12:14
          3
          • R raven-worx
            13 Oct 2016, 11:14

            @MrLibya

            qDebug() << query.lastError().text()
            

            should give more hints whats the problem.

            But i guess the problem is the table name ([main].[items]), which isn't valid?!?

            M Offline
            M Offline
            MrLibya
            wrote on 13 Oct 2016, 12:14 last edited by
            #5

            @raven-worx ok i got

            Parameter count mismatch
            
            1 Reply Last reply
            1
            • M Offline
              M Offline
              MrLibya
              wrote on 13 Oct 2016, 13:20 last edited by
              #6

              i also tried

                      query.addBindValue(ui->Numberinput->text());
                      query.addBindValue(ui->search_item->text());
              

              but the same

              R 1 Reply Last reply 13 Oct 2016, 13:40
              0
              • M MrLibya
                13 Oct 2016, 13:20

                i also tried

                        query.addBindValue(ui->Numberinput->text());
                        query.addBindValue(ui->search_item->text());
                

                but the same

                R Offline
                R Offline
                raven-worx
                Moderators
                wrote on 13 Oct 2016, 13:40 last edited by
                #7

                @MrLibya
                this error is very unspecific and may also occur when the query itself is invalid.
                As i already stated, whats about the table name [main].[items]. Is this really correct?!

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                M 1 Reply Last reply 13 Oct 2016, 13:46
                0
                • R raven-worx
                  13 Oct 2016, 13:40

                  @MrLibya
                  this error is very unspecific and may also occur when the query itself is invalid.
                  As i already stated, whats about the table name [main].[items]. Is this really correct?!

                  M Offline
                  M Offline
                  MrLibya
                  wrote on 13 Oct 2016, 13:46 last edited by MrLibya
                  #8

                  @raven-worx yep , i've already used this method in another function and it's work very well

                  void MainWindow::UpdateSearchItem(QStringList *List)
                  {
                      QSqlQuery query("SELECT * FROM [main].[items]");
                      while (query.next())
                          *List << query.value(1).toString();
                  }
                  
                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    hskoglund
                    wrote on 13 Oct 2016, 14:18 last edited by
                    #9

                    Hi, maybe the text substitution goes haywire, to see the resolved text, try:
                    qDebug() << query.executedQuery();

                    M 1 Reply Last reply 13 Oct 2016, 14:26
                    1
                    • H hskoglund
                      13 Oct 2016, 14:18

                      Hi, maybe the text substitution goes haywire, to see the resolved text, try:
                      qDebug() << query.executedQuery();

                      M Offline
                      M Offline
                      MrLibya
                      wrote on 13 Oct 2016, 14:26 last edited by
                      #10

                      @hskoglund said in QSqlQuery select statment:

                      qDebug() << query.executedQuery();

                      i got

                      "SELECT * FROM [main].[items] WHERE barcode = ? OR name = ? "
                      
                      H 1 Reply Last reply 13 Oct 2016, 17:32
                      0
                      • M MrLibya
                        13 Oct 2016, 14:26

                        @hskoglund said in QSqlQuery select statment:

                        qDebug() << query.executedQuery();

                        i got

                        "SELECT * FROM [main].[items] WHERE barcode = ? OR name = ? "
                        
                        H Offline
                        H Offline
                        hskoglund
                        wrote on 13 Oct 2016, 17:32 last edited by
                        #11

                        Indeed the text substitution seems borked, you could try instead doing the text substitution directly yourself, something like this:

                        QSqlQuery query("SELECT * FROM [main].[items] WHERE barcode = '123' OR name = 'Smith'");
                        
                        M 1 Reply Last reply 13 Oct 2016, 19:51
                        0
                        • H hskoglund
                          13 Oct 2016, 17:32

                          Indeed the text substitution seems borked, you could try instead doing the text substitution directly yourself, something like this:

                          QSqlQuery query("SELECT * FROM [main].[items] WHERE barcode = '123' OR name = 'Smith'");
                          
                          M Offline
                          M Offline
                          MrLibya
                          wrote on 13 Oct 2016, 19:51 last edited by MrLibya
                          #12

                          @hskoglund that will not really helps , goes the barcode - name , the user will input from line edit , so dose this only not work on sqlite ? if i use mysql it will work ??

                          1 Reply Last reply
                          0
                          • H Offline
                            H Offline
                            hskoglund
                            wrote on 13 Oct 2016, 20:05 last edited by
                            #13

                            Hi, you can build the select string something like this:

                            QString s = QString("SELECT * FROM [main].[items] WHERE barcode = '%1' OR name = '%2'").arg(ui->Numberinput->text()).arg(ui->search_item->text());
                            QSqlQuery query(s);
                            
                            M 1 Reply Last reply 13 Oct 2016, 20:38
                            2
                            • M Offline
                              M Offline
                              MrLibya
                              wrote on 13 Oct 2016, 20:36 last edited by
                              #14
                              This post is deleted!
                              1 Reply Last reply
                              0
                              • H hskoglund
                                13 Oct 2016, 20:05

                                Hi, you can build the select string something like this:

                                QString s = QString("SELECT * FROM [main].[items] WHERE barcode = '%1' OR name = '%2'").arg(ui->Numberinput->text()).arg(ui->search_item->text());
                                QSqlQuery query(s);
                                
                                M Offline
                                M Offline
                                MrLibya
                                wrote on 13 Oct 2016, 20:38 last edited by
                                #15

                                @hskoglund thx it work :)

                                1 Reply Last reply
                                0

                                7/15

                                13 Oct 2016, 13:40

                                • Login

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