Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Qt 5.2 msvc x64] QMYSQL invalid driver handle



  • Hello all, as this is my first post :)
    Ad meritum:
    QMYSQL driver return incorrect driver handle (i think), as following snippet throws "read access voilation" exception with mysql_ping function:
    @
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "initialConnection");
    if(!db.isValid())
    {
    qCritical() << "Initial connection invalid!";
    return QStringList();
    }
    db.setHostName("192.168.1.2");
    db.setPort(3306);
    db.setUserName("kOf_GetUsers");
    db.setPassword("");
    db.setDatabaseName("kOferta");
    db.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1;MYSQL_OPT_RECONNECT=1");

    qDebug() << "connName:" << db.connectionName();
    qDebug() << "driver:" << db.driverName();
    qDebug() << "options:" << db.connectOptions();
    qDebug() << "host:" << db.hostName();
    qDebug() << "database error:" << db.lastError().databaseText();
    qDebug() << "driver error:" << db.lastError().driverText();

    QSqlDriver* driver = db.driver();
    qDebug() << "driver info";
    driver->dumpObjectInfo();
    driver->dumpObjectTree();

    QVariant v = driver->handle();
    if (v.isValid() && qstrcmp(v.typeName(), "MYSQL*")==0)
    {
    qDebug() << "v.typename:" << v.typeName();

    MYSQL *handle = static_cast<MYSQL *>(v.data());
    if (handle != NULL)
    {
        qDebug() << "handle:" << handle;
        qDebug() << "mysql error:" << mysql_error(handle);
    
        try
        {
            qDebug() << "mysql ping:" << mysql_ping(handle);
            qDebug() << "mysql error:" << mysql_error(handle);
    
    //                mysql_ssl_set(handle, ":/client-key", ":/client-cert", ":/ca-cacert", NULL, NULL);
     //       mysql_ssl_set(handle, "D:/git/build-kOferta/client-key.pem", "D:/git/build-kOferta/client-cert.pem", "D:/git/build-kOferta/ca-cacert.pem", "D:/git/build-kOferta/", "DHE-RSA-AES256-SHA");
        }
        catch (std::exception& e)
        {
            qCritical() << "[mysql_ssl_set] Standard exception: " << e.what();
        }
        catch(...)
        {
            qCritical() << "[mysql_ssl_set] Unknown exception";
        }
    }
    

    }
    @
    And my log is as follows:
    @
    [28.12.2013 11:09:47]: connName: "initialConnection"
    [28.12.2013 11:09:47]: driver: "QMYSQL"
    [28.12.2013 11:09:47]: options: "CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1;MYSQL_OPT_RECONNECT=1"
    [28.12.2013 11:09:47]: host: "192.168.1.2"
    [28.12.2013 11:09:47]: database error: ""
    [28.12.2013 11:09:47]: driver error: ""
    [28.12.2013 11:09:47]: driver info
    [28.12.2013 11:09:47]: OBJECT QMYSQLDriver::unnamed
    [28.12.2013 11:09:47]: SIGNALS OUT
    [28.12.2013 11:09:47]: <None>
    [28.12.2013 11:09:47]: SIGNALS IN
    [28.12.2013 11:09:47]: <None>
    [28.12.2013 11:09:47]: QMYSQLDriver::
    [28.12.2013 11:09:47]: v.typename: MYSQL*
    [28.12.2013 11:09:47]: handle: 0x2c99f0
    [28.12.2013 11:09:47]: mysql error:
    @

    What else can i check? What may be the cause? I use libmysql from MySQL Connector C 6.1 6.1.2 - i dont know if it is relevant...

    Best regards, konserw



  • @ MYSQL *handle = *static_cast<MYSQL **>(v.data());@
    and
    @MYSQL *handle = static_cast<MYSQL *>(v.data());@
    seems to be different lines according to docs (different pointer addresses).
    Could you try another variant?



  • if we have CustomType inside Qvariant, type() will be CustomType and .data() return pointer to CustomType.
    If your type is pointer to MYSQL... maybe that is the point?



  • Thank you very much. Casting to MYSQL** helped.
    I wonder when this was changed, becouse cast to MYSQL* worked in qt 4.8...



  • Ok, not realy helped, because dereferenced MYSQL** is NULL, so either thios cast is wrong, or something with my database connection code :/
    Could anyone help me with that? Mabey some working example, how this should be done?





  • I've used this patch/diff from post above on QT 5.2. Then I've recompiled QT and seems working, but I have to made some tests to make sure that everything is fine. konserw if you want you can email to me

    KR Dawid


Log in to reply