1

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 ?

Raiden Core
  • 1,535
  • 12
  • 13

1 Answers1

1

Yes, it is QTBUG-47784 and related QTBUG-47452 bugs. Both was fixed in Qt 5.5.1. Unfortunately there is no solution without changing the source code of Qt or upgrading to 5.5.1.

Nick Bondarenko
  • 6,211
  • 4
  • 35
  • 56
  • Thanks, it is done, updated to 5.5.1 problem solved. But may I asked how programmers were connecting to mysql before 5.5.1 ? hmmmmmmmmmm qt ?! – Raiden Core Dec 16 '15 at 18:44
  • This bug was only in 5.5.0 release. In previous versions all was fine. And i think somebody as i am for example just patch qt sources and recompile with manual fix. – Nick Bondarenko Dec 16 '15 at 20:32