Connection to a MySQL database with Qt Creator on Windows
-
wrote on 20 Dec 2017, 17:58 last edited by A Former User
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]
-
wrote on 20 Dec 2017, 18:10 last edited by
@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 said in Connection to a MySQL database with Qt Creator on Windows:
QMYSQL
Hi
Did you try with fresh install of Qt?wrote on 20 Dec 2017, 18:17 last edited by@Advanced
No.
But I've uninstalled and reinstalled Qt.This night, I'll install the new Qt 5.10 and try again.
Thanks.
-
wrote on 20 Dec 2017, 18:21 last edited by
It's probably fixed your issue, but i recommend to you if you want to sure about stability used latest LTS version of Qt that it's 5.9.3
-
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]
wrote on 20 Dec 2017, 18:43 last edited by@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 ?
-
wrote on 20 Dec 2017, 22:57 last edited by
@SGaist
Yes, I think -
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.
-
wrote on 21 Dec 2017, 14:01 last edited by
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...
-
wrote on 27 Dec 2017, 17:40 last edited by
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.
1/11