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

MySQL and lastError

Scheduled Pinned Locked Moved Solved General and Desktop
mysql
10 Posts 3 Posters 5.7k Views 3 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.
  • B Offline
    B Offline
    brunjak
    wrote on 25 Mar 2016, 21:44 last edited by
    #1

    Hi,
    I have a MySQL db running on my NAS (MariaDB). By using MySQL Workbench (6.3) it's possible to manage and edit the database. The next step would be to connect to the database by QT. I wrote the following code:

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QString serverName = "192.168.1.34";
    QString dbName = "tvb_mitgliederliste";
    
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setConnectOptions();
    
    db.setHostName(serverName);
    db.setDatabaseName(dbName);
    db.setPort(3306);
    db.setUserName("tvb");
    db.setPassword("tvb");
    
    if(db.open())
    {
        qDebug() << "Opend!";
        qDebug() << "Error = " << db.lastError().text();
        db.close();
    }
    else
    {
        qDebug() << "Error = " << db.lastError().text();
    }
    
    return a.exec();
    

    }

    The output on the console is:
    Opend!
    Error = ""
    The capturing result by wireshark also seems correct. If I test with a wrong password the output on the console is the same as above! db.open() returns true! The capture with wiresharks says: Response Error 1045 (Acces denied for user 'tvb'@192.168.0.11' (using password: YES).

    Any idea why db.lastError().text() returns no error instead of Error 1045 or something like that?

    Thank you
    Jakob

    B 2 Replies Last reply 27 Mar 2016, 12:48
    0
    • K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 25 Mar 2016, 22:22 last edited by
      #2

      @brunjak

      What does db.isValid() return? Do you have anything printed in the debug output window?

      Read and abide by the Qt Code of Conduct

      B 1 Reply Last reply 25 Mar 2016, 22:45
      0
      • K kshegunov
        25 Mar 2016, 22:22

        @brunjak

        What does db.isValid() return? Do you have anything printed in the debug output window?

        B Offline
        B Offline
        brunjak
        wrote on 25 Mar 2016, 22:45 last edited by brunjak
        #3

        @kshegunov
        Thank you for your response. isValid returns true (in both cases: correct and incorrect password). I can't find any warning or error in the application output.

        Thank you
        Jakob

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 25 Mar 2016, 23:53 last edited by
          #4

          Hi,

          What version of Qt are you using ? There was an initialization bug in older version that would make the open call return true even in case of failure. This has been fixed in between so updating Qt would be the thing to do. Otherwise you'll have to compile Qt yourself after applying the patch by hand.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          B 1 Reply Last reply 27 Mar 2016, 12:33
          1
          • S SGaist
            25 Mar 2016, 23:53

            Hi,

            What version of Qt are you using ? There was an initialization bug in older version that would make the open call return true even in case of failure. This has been fixed in between so updating Qt would be the thing to do. Otherwise you'll have to compile Qt yourself after applying the patch by hand.

            B Offline
            B Offline
            brunjak
            wrote on 27 Mar 2016, 12:33 last edited by
            #5

            @SGaist
            Hi,
            I'm using Qt 5.5.0 (MSVC 2013, 32 bit).

            Thank you
            Jakob

            1 Reply Last reply
            0
            • B brunjak
              25 Mar 2016, 21:44

              Hi,
              I have a MySQL db running on my NAS (MariaDB). By using MySQL Workbench (6.3) it's possible to manage and edit the database. The next step would be to connect to the database by QT. I wrote the following code:

              int main(int argc, char *argv[])
              {
              QCoreApplication a(argc, argv);

              QString serverName = "192.168.1.34";
              QString dbName = "tvb_mitgliederliste";
              
              QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
              db.setConnectOptions();
              
              db.setHostName(serverName);
              db.setDatabaseName(dbName);
              db.setPort(3306);
              db.setUserName("tvb");
              db.setPassword("tvb");
              
              if(db.open())
              {
                  qDebug() << "Opend!";
                  qDebug() << "Error = " << db.lastError().text();
                  db.close();
              }
              else
              {
                  qDebug() << "Error = " << db.lastError().text();
              }
              
              return a.exec();
              

              }

              The output on the console is:
              Opend!
              Error = ""
              The capturing result by wireshark also seems correct. If I test with a wrong password the output on the console is the same as above! db.open() returns true! The capture with wiresharks says: Response Error 1045 (Acces denied for user 'tvb'@192.168.0.11' (using password: YES).

              Any idea why db.lastError().text() returns no error instead of Error 1045 or something like that?

              Thank you
              Jakob

              B Offline
              B Offline
              brunjak
              wrote on 27 Mar 2016, 12:48 last edited by
              #6

              Hi,
              I've augmented the code by executing a simple query:

              if(db.open()&& db.isValid())
              {
              qDebug() << "Opend!";

                  QSqlQuery qry;
              
                  if(qry.exec("SELECT * FROM tvb_mitgliederliste.person;"))
                  {
                      while(qry.next())
                      {
                          qDebug() << qry.value(3).toString();
                      }
                  }
                  else
                  {
                      qDebug() << "Error = " << db.lastError().text();
                  }
              
                  qDebug() << "Closing ...";
                  db.close();
              }
              else
              {
                  qDebug() << "Error = " << db.lastError().text();
              }
              

              With the correct password the output is correct. With the wrong password the query can't be executed but db.open and db.isValid are "true".

              Thank you

              K 1 Reply Last reply 27 Mar 2016, 13:08
              0
              • B brunjak
                27 Mar 2016, 12:48

                Hi,
                I've augmented the code by executing a simple query:

                if(db.open()&& db.isValid())
                {
                qDebug() << "Opend!";

                    QSqlQuery qry;
                
                    if(qry.exec("SELECT * FROM tvb_mitgliederliste.person;"))
                    {
                        while(qry.next())
                        {
                            qDebug() << qry.value(3).toString();
                        }
                    }
                    else
                    {
                        qDebug() << "Error = " << db.lastError().text();
                    }
                
                    qDebug() << "Closing ...";
                    db.close();
                }
                else
                {
                    qDebug() << "Error = " << db.lastError().text();
                }
                

                With the correct password the output is correct. With the wrong password the query can't be executed but db.open and db.isValid are "true".

                Thank you

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 27 Mar 2016, 13:08 last edited by
                #7

                @brunjak
                Well, beside the fact that db.isValid() should be on the left of db.open() (logical operators have left associativity) I see nothing that'd hint why you get that strange behavior.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • B brunjak
                  25 Mar 2016, 21:44

                  Hi,
                  I have a MySQL db running on my NAS (MariaDB). By using MySQL Workbench (6.3) it's possible to manage and edit the database. The next step would be to connect to the database by QT. I wrote the following code:

                  int main(int argc, char *argv[])
                  {
                  QCoreApplication a(argc, argv);

                  QString serverName = "192.168.1.34";
                  QString dbName = "tvb_mitgliederliste";
                  
                  QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
                  db.setConnectOptions();
                  
                  db.setHostName(serverName);
                  db.setDatabaseName(dbName);
                  db.setPort(3306);
                  db.setUserName("tvb");
                  db.setPassword("tvb");
                  
                  if(db.open())
                  {
                      qDebug() << "Opend!";
                      qDebug() << "Error = " << db.lastError().text();
                      db.close();
                  }
                  else
                  {
                      qDebug() << "Error = " << db.lastError().text();
                  }
                  
                  return a.exec();
                  

                  }

                  The output on the console is:
                  Opend!
                  Error = ""
                  The capturing result by wireshark also seems correct. If I test with a wrong password the output on the console is the same as above! db.open() returns true! The capture with wiresharks says: Response Error 1045 (Acces denied for user 'tvb'@192.168.0.11' (using password: YES).

                  Any idea why db.lastError().text() returns no error instead of Error 1045 or something like that?

                  Thank you
                  Jakob

                  B Offline
                  B Offline
                  brunjak
                  wrote on 27 Mar 2016, 16:18 last edited by
                  #8

                  Hi,
                  perhaps I'm doing something wrong by including mysql-driver (INCLUDEPATH, LIBS ) . Here is my .pro file:

                  QT       += core
                  QT       += sql
                  QT       -= gui
                  
                  INCLUDEPATH += "C:\Program Files (x86)\MySQL\MySQL Server 5.7\include"
                  LIBS += -L"C:\Program Files (x86)\MySQL\MySQL Server 5.7\lib"
                  
                  TARGET = DBApp
                  CONFIG   += console
                  CONFIG   -= app_bundle
                  
                  TEMPLATE = app
                  
                  
                  SOURCES += main.cpp
                  

                  I don't knwo what would be the best practice to include the driver.

                  Thank you
                  Jakob

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 27 Mar 2016, 20:37 last edited by
                    #9

                    IIRC, the fix went in for 5.5.1. So you should update to that version if possible. If not I can point you to the corresponding patch.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    B 1 Reply Last reply 5 May 2016, 16:49
                    0
                    • S SGaist
                      27 Mar 2016, 20:37

                      IIRC, the fix went in for 5.5.1. So you should update to that version if possible. If not I can point you to the corresponding patch.

                      B Offline
                      B Offline
                      brunjak
                      wrote on 5 May 2016, 16:49 last edited by
                      #10

                      @SGaist

                      ... thank you. With QT 5.6.0 it's running.

                      Jakob

                      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