Connection to a MySQL database with Qt Creator on Windows
-
Hi everybody.
I have one problem and I need your help.
I'm working on a Qt project where I need to connect the software to a database.
I try this code in my main code sourcebool createConnection() { QSqlDatabase laBase = QSqlDatabase::addDataBase("QMYSQL"); laBase.setHostName("localhost"); laBase.setDataBaseName("test"); laBase.setUserName("justin"); laBase.setPassword("******"); if (!laBase.open()){ QMessageBox:: critical (0 /* ... */ ); QObject::tr("Error"), laBase.lastError().text()); return false; } return true; }
But when I run it, I have a message box with this characteristics:
Title of the message box: Error
Content: Driver not loaded Driver not loadedAnd I have this message error
QSqlDatabase : QMYSQL driver not loaded QSqlDatabase : available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
I've a similar error when I use QSQLITE.
I'm on Windows 10, 64 bits
With Qt Creator 4.0.2 based on Qt 5.7.0I'll be very happy to be helped.
Thanks.
[Edit: added code tags ~~ @Wieland]
-
@Justin_Ahinon said in Connection to a MySQL database with Qt Creator on Windows:
QMYSQL
Hi
Did you try with fresh install of Qt? -
@Justin_Ahinon
If this error occurred also with SQLITE, then check if you have in your *.pro file something like this:QT += sql
-
Hi,
To add to my fellow, do you have the MySQL client libraries available on your system ?
-
Run your application with the QT_DEBUG_PLUGINS environment variable set to 1 to see what is happening.
You can set that variable in the Run part of the project panel.
-
I would make sure you complie the mysql plugin..
- make sure you have installed mysql the same lib headers for x86 or x64.
- open mysql plugin located in qbase you will have to comment out a few things as they still havnt fix the bug yet.
- copy those libs and dlls into your complier folder
- copy mysqlclient.dll and qt5sql.dll into your .exe folder
- should be all set...
-
Hi every body,
I solve the problem by using the QSQLITE driver which is installed with Qt./.pro file QT += widgets sql
//.cpp file bool createConnection() { QSqlDatabase laBase = QSqlDatabase::addDataBase("QSQLITE"); laBase.setHostName("localhost"); laBase.setDataBaseName("test"); laBase.setUserName("justin"); laBase.setPassword("******"); if (!laBase.open()){ QMessageBox:: critical (0 /* ... */ ); QObject::tr("Error"), laBase.lastError().text()); return false; } return true; }
-
Hi,
Are you aware of the implications of moving from MySQL to SQLite ?
One of which is: you are using a relative path for your SQLite database which won't work if your users install your application in e.g.
Program Files
as it's a read-only location.