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
Forum Updated to NodeBB v4.3 + New Features

SQL query not working

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqlqt 5.7
10 Posts 6 Posters 5.0k Views 4 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.
  • R Offline
    R Offline
    ronyNS
    wrote on 2 Oct 2016, 10:12 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 2 Oct 2016, 10:18 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
      • R Offline
        R Offline
        ronyNS
        wrote on 2 Oct 2016, 10:28 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.

        M 1 Reply Last reply 2 Oct 2016, 10:31
        0
        • R ronyNS
          2 Oct 2016, 10:28

          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.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 2 Oct 2016, 10:31 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
          • R Offline
            R Offline
            ronyNS
            wrote on 2 Oct 2016, 10:34 last edited by
            #5

            Yes i cross checked everything . Everything is perfect

            M 1 Reply Last reply 2 Oct 2016, 10:38
            0
            • R ronyNS
              2 Oct 2016, 10:34

              Yes i cross checked everything . Everything is perfect

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 2 Oct 2016, 10:38 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
              • P Offline
                P Offline
                p3c0
                Moderators
                wrote on 2 Oct 2016, 10:45 last edited by p3c0 10 Feb 2016, 10:48
                #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 2 Oct 2016, 18:37 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
                  • P Offline
                    P Offline
                    Pradeep Kumar
                    wrote on 2 Oct 2016, 19:27 last edited by Pradeep Kumar 10 Mar 2016, 16:26
                    #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 3 Oct 2016, 13:41 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

                      2/10

                      2 Oct 2016, 10:18

                      topic:navigator.unread, 8
                      • Login

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