Solved MySQL and lastError
-
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 -
What does
db.isValid()
return? Do you have anything printed in the debug output window? -
@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 -
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.
-
@SGaist
Hi,
I'm using Qt 5.5.0 (MSVC 2013, 32 bit).Thank you
Jakob -
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
-
@brunjak
Well, beside the fact thatdb.isValid()
should be on the left ofdb.open()
(logical operators have left associativity) I see nothing that'd hint why you get that strange behavior. -
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 -
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.
-