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. SQL query not working
QtWS25 Last Chance

SQL query not working

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqlqt 5.7
10 Posts 6 Posters 4.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.
  • ronyNSR Offline
    ronyNSR Offline
    ronyNS
    wrote on last edited by
    #1
    QSqlQuery a;
        QString b;
        a.exec("Select Stock From Manufacturer_stock Where Product=Parle");
        if (a.next()) {
    
                b = a.value(0).toString();
                ui->label_2->setText(b);
               }
    

    This code does nothing. What is wrong?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      The syntax is not valid i think
      a.exec("Select Stock From Manufacturer_stock Where Product='Parle' ");
      ( add single quotes around text values )

      If you are using SQLite then
      http://sqlitebrowser.org/
      Is nice tools as you can test your SQL there and easy work with the database.

      1 Reply Last reply
      4
      • ronyNSR Offline
        ronyNSR Offline
        ronyNS
        wrote on last edited by
        #3

        I am using Access and the code is swill not working
        i tried with quotes
        i dont know whats wrong. All other queries are working fine.

        mrjjM 1 Reply Last reply
        0
        • ronyNSR ronyNS

          I am using Access and the code is swill not working
          i tried with quotes
          i dont know whats wrong. All other queries are working fine.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @ronyNS
          Well are you sure it will get any hits then?
          and Column is called Stock ?

          1 Reply Last reply
          2
          • ronyNSR Offline
            ronyNSR Offline
            ronyNS
            wrote on last edited by
            #5

            Yes i cross checked everything . Everything is perfect

            mrjjM 1 Reply Last reply
            0
            • ronyNSR ronyNS

              Yes i cross checked everything . Everything is perfect

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @ronyNS

              You can try
              a.exec("Select Stock From Manufacturer_stock Where Product like 'P%' ");

              And see if it matches Parla.

              Else i am out of guesses. Did Not use Access since 1997 :)

              1 Reply Last reply
              2
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by p3c0
                #7

                lastError should give you the exact cause of error.
                Additionally check if the database is opened using isOpen or errors if any.

                157

                1 Reply Last reply
                5
                • M Offline
                  M Offline
                  mjsurette
                  wrote on last edited by
                  #8

                  I believe that the SQL standard requires a trailing semicolon (;) but some databases/drivers choke on it. Try

                  a.exec("Select Stock From Manufacturer_stock Where Product='Parle';");
                  

                  Mike

                  1 Reply Last reply
                  0
                  • Pradeep KumarP Offline
                    Pradeep KumarP Offline
                    Pradeep Kumar
                    wrote on last edited by Pradeep Kumar
                    #9

                    HI

                    Can u try with this, hope this works for you.

                    QSqlQuery query;
                    query.prepare("Select Stock From Manufacturer_stock Where Product='Parle'; ");
                    query.exec();
                    while(query.next())
                    {
                        QString firstValue = query.value(0).toString();
                        qDebug() << "first value :" << firstValue  << endl;
                    
                        QString secondValue = query.value(1).toString();
                        qDebug() << "second value :" << secondValue  << endl;
                     }
                    

                    and provide debug statements,
                    As mentioned by @p3c0 check whether the dB is opened.

                    Pradeep Kumar
                    Qt,QML Developer

                    1 Reply Last reply
                    1
                    • T Offline
                      T Offline
                      Tschikkn
                      wrote on last edited by
                      #10

                      Hi!

                      You should get the error-message as already mentioned above:

                      QSqlQuery a;
                      if( !a.exec("Select Stock From Manufacturer_stock Where Product='Parle';") ) {
                          qDebug() << "Error:" << a.lastError().text();
                          }
                      
                      
                      1 Reply Last reply
                      1

                      • Login

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