Reputation: 14593
I am trying to implement a database system in my Qt application. For this purpose, I am trying to use MySQL (with the related QMYSQL
driver). My class connection-function is written below.
Assuming that connection
is defined as a private
class member:
private:
QSqlDatabase connection;
we have the following:
database::database() : connection() {
this->connection.addDatabase("QMYSQL");
this->connection.setHostName(p.database->server_addr);
this->connection.setUserName(p.database->username);
this->connection.setPassword(p.database->password);
this->connection.setDatabaseName(p.database->database_name);
if (!connection.open())
this->error = this->connection.lastError().text();
else this->error = "";
}
I get database::error
with the value Driver not loaded Driver not loaded
(yes, it's written twice). I've seen here on Stack Overflow that I have to put the driver libraries in my application's path. I've done this and nothing happened. Below is a screenshot of the Qt libraries that are in my path.
EDIT
After calling QSqlDatabase::drivers()
, I found out that I have the drivers available. Below is the output ( int the show-more section of the message-box).
Upvotes: 2
Views: 15581
Reputation: 1113
I have encountered this problem many times. If you are in
LINUX: You need to build it. You need to first install MySQL server then MySQL Client from package manager. then follow instructions in Qt Documentation website Here. Which is basically these commands:
cd $QTDIR/qtbase/src/plugins/sqldrivers/mysql
make install
and after the build process finishes, generated plugin will in $QTDIR/qtbase/src/plugins/sqldrivers/
i guess.
WINDOWS: No need to build. Install MySQL and after that you can
also, if you have done all above and still doesn't work, you need to install Visual C++ Runtime Libraries. I had this problem in XP and Dependency Walker didn't show any missing or incompatible .dll file and after installing those runtime libs my problem was solved. Where you can find them packed and ready? Here
Before you test your app, its a good idea to reboot so those changes in PATH and ... can take effect.
Upvotes: 0
Reputation: 1322
I use msys2 with its qt5 package mingw64/mingw-w64-x86_64-qt5 (5.8.0-3) and looking at the plugins/sqldrivers/qsqlmysql.dll with the Dependency Walker reveled dependency on the mariadb.dll, so I installed the mariadb client package:
pacman -S mingw64/mingw-w64-x86_64-libmariadbclient
and the dll appeared in /mingw64/bin/mariadb.dll, so I just copied the dll next to my app's exe file and it suddenly worked.
Even if you don't use msys2, you should still look at your qsqlmysql.dll with Dependency Walker and provide the missing dlls to your app. You can install MariaDB or MySQL connectors and just copy the dlls from the installed path.
Upvotes: 1
Reputation: 708
For mingw and Windows: Download the C (not c++) connector from: https://dev.mysql.com/downloads/connector/c/, then copy the library: libmysql.dll, to the folder: D:\Qt\5.5\mingw492_32\bin. This should solve the problem for the not loaded driver.
Upvotes: 6
Reputation: 11
Please see the link for QT5: http://seppemagiels.com/blog/create-mysql-driver-qt5-windows => qsqlmysql.dll and libmysql.dll (and libmysql.lib, if your installation of MySQL has it) are needed
And the link for QT4: http://seppemagiels.com/blog/create-mysql-driver-qt-windows => libqsqlmysql4.a and qsqlmysql4.dll and libmysql.dll (and libmysql.lib, if your installation of MySQL has it) are needed
Upvotes: 1
Reputation: 14593
In addition to @OnWhenReady's answer, I run the DependencyWalker on the qsqlmysql.dll
and found that there were missing some dependencies.
IEShims.dll
I copied it in my path and now it's working!
As a tip: run DependecyWalker for this kind of libraries, because, as it's written in the Qt Documentation, QMYSQL driver libraries won't give errors (won't show them).
Upvotes: 1
Reputation: 949
1) I assume that you already compiled your qsqlmysql dll. You should have the qmyssql.dll
in the plugins/sqldrivers
directory relative to your binary path of your executable.
2) You should also be careful not to mix release and debug dlls (the ones with the d
at the end).
3) The static method addDatabase should be used this way: this->connection = QSqlDatabase::addDatabase(DRIVER, NAME);
4) Another point: The reason you get the error "twice" (you actually just get two errors combined) is that connection.lastError().text()
results a combined error message (appended) from the driver and from the connection attempt (see the API more more information about the differences).
5) When i took a closer look at your libs i could not see the non-debug version of libmysql.dll
. If you run in release mode you must use the release library since the runtime will look for libmysql.dll
and not libmysqld.dll
. This will give you this error.
Upvotes: 3