1
votes

I have successfully compiled the MySQL drivers, but I cannot force Qt to load them.

What should be said in the beginning Iv got ODBC and SQLite drivers with the package (after installing Qt) and they are not detected either.

In *C:\Qt\4.8.0\plugins\sqldrivers* Iv got:

qsqlite4.dll

qsqlite4.lib

qsqlited4.dll

qsqlited4.lib

qsqlmysql4.dll

qsqlmysql4.lib

qsqlmysqld4.dll

qsqlmysqld4.lib

qsqlodbc4.dll

qsqlodbc4.lib

qsqlodbcd4.dll

qsqlodbcd4.lib

qsqlpsql4.dll

qsqlpsql4.lib

qsqlpsqld4.dll

qsqlpsqld4.lib

The qsqlmysql*.* files I have also put in:

C:\Qt\4.8.0\bin

APPLICATION\

APPLICATION\Debug\

APPLICATION\Release\

APPLICATION\sqldrivers\

Of course Iv got QtSql*.dll also in place.

Iv got .pro properly configured:

QT += core gui network sql

And I'm running this code:

#include <QSqlRecord>
#include <QSqlError>

QLibrary mysqllib("qsqlmysqld4.dll");
mysqllib.load();
auto t1 = mysqllib.isLoaded();
qDebug()<<"my library loaded"<<mysqllib.isLoaded();

QPluginLoader plug("qsqlmysqld4.dll");
plug.load();
auto t2 = plug.isLoaded();
qDebug()<<"mysql plugin is loaded"<<plug.isLoaded();

ui->textEditContent->append( "--SQL DRIVERS SUPPORTED:--\n" );
FOREACH( auto driver, QSqlDatabase::drivers() )
    ui->textEditContent->append( "  " + driver + "\n" );

QLibrary and QPluginLoader returns true. And QSqlDatabase::drivers() is empty. What I'm doing wrong? None of the drivers in C:\Qt\4.8.0\plugins\sqldrivers are seen by Qt. Iv compiled Qt and SQL drivers with same compiler (MSVC2010), without errors. I'm running my code for several months. The task was to add MySQL support.

2

2 Answers

3
votes

Problem solved! I have modified the code, now it looks like this:

#include <QSqlRecord>
#include <QSqlError>    

QStringList liblist;
liblist.push_back("c:/Qt/4.8.0/plugins/");
liblist.push_back("c:/Qt/4.8.0/bin/");
QCoreApplication::setLibraryPaths(liblist);

QLibrary mysqllib("qsqlmysqld4.dll");
mysqllib.load();
auto t1 = mysqllib.isLoaded();
qDebug()<<"my library loaded"<<mysqllib.isLoaded();

QPluginLoader plug("qsqlmysqld4.dll");
plug.load();
auto t2 = plug.isLoaded();
qDebug()<<"mysql plugin is loaded"<<plug.isLoaded();

ui->textEditContent->append( "--SQL DRIVERS SUPPORTED:--\n" );
FOREACH( auto driver, QSqlDatabase::drivers() )
    ui->textEditContent->append( "  " + driver + "\n" );

It seems that even if Qt have paths to it's own folder you need to specify them before loading the drivers. You can do this by adding those lines:

QStringList liblist;
liblist.push_back("c:/Qt/4.8.0/plugins/");
liblist.push_back("c:/Qt/4.8.0/bin/");
QCoreApplication::setLibraryPaths(liblist);

If you want to include those drivers into your application folder as I do you create folder 'sqldrivers' in root of your application and add path to this root. So we have libmysql.dll and qsqlmysql.dll in:

X:\APPLICATION\sqldrivers\

and code looks like this:

QStringList liblist;
liblist.push_back(QDir::currentPath()); // Qt always looks for those drivers in <LIB_FOLDER_SPECIFIED>/sqldrivers/
//liblist.push_back("e:/Qt/4.8.0/bin/");
//liblist.push_back("e:/Qt/4.8.0/plugins/");
QCoreApplication::setLibraryPaths(liblist);
0
votes

Also you need a libmysql.dll in

C:\Qt\4.8.0\bin

APPLICATION\

APPLICATION\Debug\

APPLICATION\Release\