Qt MySQL Support
-
Hi All, I would like to build a tool using QT which can query an existing MySQL database on a local server. I found this example, but am having trouble getting it to work. After running the example code, I see the error message shown below. Does anyone know what next steps should be to get this to work? Thank you.
#include <QCoreApplication> #include <QApplication> #include <QtSql/QSql> #include <QtSql/QSqlDatabase> #include <QtSql/QSqlDriver> #include <QtSql/QSqlQuery> #include <QDebug> #include <QMessageBox> #include <QSqlError> bool createConnection(); int main(int argc, char *argv[]) { QApplication app(argc, argv); if (!createConnection()){ qDebug() << "Not connected!"; return 1; } else{ qDebug() << "Connected!"; QSqlQuery query; query.exec("SELECT name FROM student"); while (query.next()) { QString name = query.value(0).toString(); qDebug() << "name:" << name; } return 0; } return app.exec(); } bool createConnection(){ QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("10.0.0.181"); db.setDatabaseName("dbname"); db.setUserName("username"); db.setPassword("password"); if (!db.open()) { qDebug() << "Database error occurred"; /* QMessageBox::critical(0, "", "Cannot open database", QMessageBox::Cancel, QMessageBox::NoButton); QMessageBox::critical(0, "", db.lastError().text(), QMessageBox::Cancel, QMessageBox::NoButton); */ return false; } return true; }
- Mike
-
Hi! This is a common problem. Please use the forum search :-)
-
@Wieland Thank you, Wieland. I have done this but am still struggling, and am hopeful for a bit of guidance. Most discussions involve solving this problem under Linux. I am looking for a Windows solution (which I did fail to mention in my post). Even a small nudge in the right direction would be appreciated.
This is the tutorial I am following. The tutorial does not say what to do if this error is shown. However, it is indicated that "libmysql.dll" must be present in the "MySQL directory", so maybe that is the problem. I have this file but the tutorial doesn't explain where it needs to be, other than the "MySQL directory" (this could be under the MySQL folder, or it could be under the QT folder, and then under any of several sub folders). It's unclear if QT is finding it but needs something else, or if this is what is needed but it can't find it. Referenced documentation mentions that there is a set of drivers which come with QT. MySQL is listed, so maybe I didn't even need to install MySQL. The error message shown above does show "QMYSQL" as having an available driver. db.lastError().text() shows "Driver not loaded Driver not loaded". Perhaps the driver exists but the path to it needs to be specified. I'm unsure where I would specify the path, though, and the tutorial doesn't do this.
These are some basic questions I am unsure how to get answers to without help.
- Mike
-
Hi,
Go to the Run part of the Project panel in Qt Creator, there modify the PATH environment variable and add the path to the folder where the MySQL .dll files can be found.
-
@SGaist Thank you! Adding the path to the folder with the MySQL dll files to the environmental variables path solved the problem.
Images showing how to add to the path variable from within QT are shown below.
The path can alternately be added using the System control panel. From the System control panel, I selected "Advanced system settings":
The "System Properties" window appered. I selected "Environmental Variables":
From the Environmental Variables window, I selected the "Path" variable and clicked "Edit...":
A list of paths appered. I clicked "New":
An edit field for a new path became available, and I pasted in the path of the folder which contained the libmysql.dll file. (Which is simply the "lib" folder underneath where I unzipped the "mysql-5.7.16-win32" download to.)
When I run the code I posted above, it now reports that it is able to connect to the database:
- Mike
-
Don't do that in your system environment variable. That's a bad habit !
-
Hi and welcome to devnet,
Please search the forum a bit, I've already provided the solution several times for OS X/macOS.