Can QSqlDatabase use MySQL?



  • Hi,

    I am very much impressed with the SQL classes and support in Qt. However, I usually work with MySQL Community Edition. Is there any support for that database? I don't see a driver for it...

    Thanks,
    Juan Dent



  • QT supports MySql community version.
    Add to your .pro file: QT += sql # that will allow the SQL library to be loaded.

    In your application do the following

    #include <QSqlDatabase>

    QSqlDatabase mdb; // instantiate a QSqlDatabase class variable

    int someFunction()
    {
    mdb = QSqldatabase::addDatabase("QMYSQL", "MyConnection"); // load the MySql driver
    mdb.setHostName(serverAddress);
    mdb.setUserName(userName);
    mdb.setPassword(passWord);
    mdb.setDatabaseName(yourDefaultDatabaseName);
    return mdb.open();
    }
    There is no attempt from me to check for errors. Kindly read the documentation.
    Once the database is open, then you can use the high level classes of QT. You will see that the query classes use QSqlDatabase as parameter.

    Hope I helped.


  • Lifetime Qt Champion

    Hi,

    In addition to what @blaisesegbeaya wrote. Here you can find the complete list of SQL drivers supported by Qt.

    Note that you need to have the MySQL client libraries installed. Depending on the version of them you have, you may have to rebuild the driver.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.