2

I need to connect to a MySQL server in a Qt application, so I wrote the following code:

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("xxx");
    db.setUserName("xxx");
    db.setPassword("xxx");
    db.setDatabaseName("xxx");
    db.open();

Unfortunately, when I'm trying to run this code right from the Qt Creator it gives me the following error right after the addDatabase function call:

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

I tried to print the last error code:

qDebug() << db.lastError().text();

Output

"Driver not loaded Driver not loaded"

Of course, I have a MySQL server installed on the same computer.

Why? What am I doing wrong? How can I fix it?

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242

1 Answers1

0

You have some things that you have to check. Check these posts, maybe they help you.

http://www.qtcentre.org/threads/55365-QSqlDatabase-QMYSQL-driver-not-loaded-but-available

QMYSQL driver available but not loaded

Community
  • 1
  • 1
drosam
  • 2,866
  • 3
  • 16
  • 18
  • I already saw them. Unfortunately, I don't have libmysqlclient.dll, only its static version (.lib file). I also tried to copy the whole `sqldrivers` directory to the `debug` folder and it didn't help – FrozenHeart Feb 03 '16 at 16:54
  • And do you have the qmyssql.dll? – drosam Feb 03 '16 at 17:08
  • Yes, in the sqldrivers directory – FrozenHeart Feb 03 '16 at 17:25
  • I think the problem is because you don´t have the `libmysql.dll`. Think that sometimes a .lib file is only a 'bridge' between the application and the .dll. Check this, maybe you don´t have seen before http://seppemagiels.com/blog/create-mysql-driver-qt5-windows – drosam Feb 03 '16 at 17:51
  • I already put libmysql.dll near the executable file and in the sqldrivers directory – FrozenHeart Feb 03 '16 at 19:51
  • And have you rebuilt your app? – drosam Feb 03 '16 at 22:17
  • Why should I rebuild an app after putting a .dll file? But yes, I already tried it – FrozenHeart Feb 04 '16 at 05:35
  • Ok, at this point I am also stuck. But the problem is with the `libmysql.dll` for sure, because Qt can find the mysql driver (`qmyssql.dll`). But I really don´t know what is going on. May be you need to recompile Qt against the right libraries, but I´m not sure. Sorry. – drosam Feb 04 '16 at 10:03