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
Qt 6.11 is out! See what's new in the release blog

QSqlQuery select statment

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
15 Posts 3 Posters 6.4k 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 MrLibya

    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
    
    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on 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
    2
    • raven-worxR raven-worx

      @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 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

      raven-worxR 1 Reply Last reply
      0
      • M MrLibya

        @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

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on 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
        3
        • raven-worxR raven-worx

          @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 last edited by
          #5

          @raven-worx ok i got

          Parameter count mismatch
          
          1 Reply Last reply
          1
          • M Offline
            M Offline
            MrLibya
            wrote on last edited by
            #6

            i also tried

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

            but the same

            raven-worxR 1 Reply Last reply
            0
            • M MrLibya

              i also tried

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

              but the same

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on 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
              0
              • raven-worxR raven-worx

                @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 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
                • hskoglundH Online
                  hskoglundH Online
                  hskoglund
                  wrote on 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
                  1
                  • hskoglundH hskoglund

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

                    M Offline
                    M Offline
                    MrLibya
                    wrote on last edited by
                    #10

                    @hskoglund said in QSqlQuery select statment:

                    qDebug() << query.executedQuery();

                    i got

                    "SELECT * FROM [main].[items] WHERE barcode = ? OR name = ? "
                    
                    hskoglundH 1 Reply Last reply
                    0
                    • M MrLibya

                      @hskoglund said in QSqlQuery select statment:

                      qDebug() << query.executedQuery();

                      i got

                      "SELECT * FROM [main].[items] WHERE barcode = ? OR name = ? "
                      
                      hskoglundH Online
                      hskoglundH Online
                      hskoglund
                      wrote on 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
                      0
                      • hskoglundH hskoglund

                        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 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
                        • hskoglundH Online
                          hskoglundH Online
                          hskoglund
                          wrote on 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
                          2
                          • M Offline
                            M Offline
                            MrLibya
                            wrote on last edited by
                            #14
                            This post is deleted!
                            1 Reply Last reply
                            0
                            • hskoglundH hskoglund

                              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 last edited by
                              #15

                              @hskoglund thx it work :)

                              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