Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...