QMYSQL driver loading error

2019-02-26 11:17发布

问题:

I tried to get MySQL running with Qt today and I am running into an error when I load the QMYSQL drivers. I am running Windows 8.1 as an OS. Here is the code of my application:

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "my_db_name");
    db.setHostName("localhost");
    db.setDatabaseName("mydb");
    db.setUserName("root");
    db.setPassword("testpwd1234");
    if (db.open()) {
        qDebug() << "Opened!";
    } else {
        qDebug() << "Error = " << db.lastError();
    }

    return a.exec();
}

I get the following error running it:

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

I found multiple solutions for the above error, none of which work for me. Here is a list of stuff I verified/tried:

  1. \Qt\5.4\mingw491_32\plugins\sqldrivers should contain qsqlmysql.dll & qsqlmysqld.dll
  2. the path to libmysql.dll & libmysqld.dll should be added to the Path variables OR contained within C:\windows\System32
  3. libmysql.dll & libmysqld.dll should be added to \Qt\5.4\mingw491_32\bin
  4. Add \Qt\5.4\mingw491_32\plugins\sqldrivers to executable directory of Qt project.
  5. Add libmysql.dll & libmysqld.dll to executable directory of Qt project.

I found 3., 4. & 5. to be a little weird, since I would expect installing everything correctly and setting Path variables should not require copying these files around. Anyway, none of the above suggestions worked for me.

I am sort of getting a little frustrated and would appreciate any help.

Thanks!

回答1:

Thanks for the input. It turned out to be a really stupid mistake. I was trying to include the 64bit binaries with my 32bit Qt build. In case anybody has troubles getting MySQL to work with Qt, here is how it worked for me:

-download/install Qt from http://www.qt.io/download/. C:\Qt\5.5\mingw492_32\plugins\sqldrivers should contain qsqlmysql.dll, which is the Qt driver for getting qt to interact with MySQL

-download/install MySQL from http://dev.mysql.com/downloads/mysql/. The windows installer worked fine for me

-install MySQL Connector C 6.1 from https://dev.mysql.com/downloads/connector/c/. This didn't come with the windows installer but will contain the necessary binary files

-copy libmysql.dll and libmysql.lib from C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib into the .exe directory for any project that has to use it

-enjoy a queryful programming session in Qt!



回答2:

It should be an mismatch version. I had similar problem in Linux.You could check the dependency of libqsqlmysql and find the exactly expected mysql version. An correctly match version should fix this problem.



标签: c++ mysql qt dll