Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

c++ - QMYSQL driver loading error

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. Qt5.4mingw491_32pluginssqldrivers 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:windowsSystem32
  3. libmysql.dll & libmysqld.dll should be added to Qt5.4mingw491_32in
  4. Add Qt5.4mingw491_32pluginssqldrivers 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:Qt5.5mingw492_32pluginssqldrivers 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)MySQLMySQL Connector C 6.1lib into the .exe directory for any project that has to use it

-enjoy a queryful programming session in Qt!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...