Important: Please read the Qt Code of Conduct -


  • I try to use sql with Qt, however, my code get the following errors:

    Available drivers:
    QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7

    Here is my original code:

    #include <QCoreApplication>
    #include <QSqlDatabase>
    #include <QDebug>
    #include <QStringList>
    #include <QSqlQuery>
    int main(int argc, char *argv[])
        QCoreApplication a(argc, argv);
        //List all available databases
        qDebug() << "Available drivers:";
        QStringList drivers = QSqlDatabase::drivers();
        for(auto t_s: drivers) {
            qDebug() << t_s;
        // open mySql
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        if(! {
            qDebug() << "Failed to connect to root mysql admin";
        } else {
            qDebug() << "open";
        QSqlQuery query(db);
        //varchar should has lenth
        query.exec("create table student(id int primary key, name varchar(20))");
        query.exec("insert into student values(1,'xiaogang')");
        query.exec("insert into student values(2,'xiaoming')");
        query.exec("insert into student values(3,'xiaohong')");
        query.exec("select id, name from student where id >= 2");
        while( {
            int value0 = query.value(0).toInt();
            QString value1 = query.value(1).toString();
            qDebug() << value0 << value1;
        return a.exec();

    Did I miss something? thank you!

  • Lifetime Qt Champion

    @LeoChen Set QT_DEBUG_PLUGINS=1 environment variable and start your app. You should then see why the plug-in was not loaded (probably MySQL client library isn't available).

  • Lifetime Qt Champion


    • Which version of MySQL are you using ?
    • Where is it installed ?

  • @SGaist

    Chens-MacBook-Pro:2017-03-31 chen$ mysql --version
    mysql  Ver 14.14 Distrib 5.7.17, for osx10.12 (x86_64) using  EditLine wrapper
    Chens-MacBook-Pro:2017-03-31 chen$ which mysql

  • Lifetime Qt Champion

    Ok, then the plugin can't find its dependencies.

    You can use install_name_tool on it to point it to your MySQL installation or rebuild the plugin following the doc recommendations.

Log in to reply