The following code works with MySql:
#include <QCoreApplication>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("192.168.139.128");
db.setDatabaseName("qsql");
db.setUserName("user");
db.setPassword("pass");
if (!db.open()) {
qDebug() << "Error = " << db.lastError();
} else {
qDebug() << "Openned!" ;
}
QSqlQuery query("SELECT id, name FROM persons");
QSqlRecord record = query.record();
while (query.next()) {
QString id = query.value(record.indexOf("id")).toString();
QString name = query.value(record.indexOf("name")).toString();
qDebug() << query.at() << ":" << id << "," << name;
}
return a.exec();
}
The problem is db.open() always returns true, no matter how wrong the connection parameters are. I am aware that this might be a known bug in Qt 5.5 (which I am using), I wonder if there is a work around or a solution for it ?