PDA

View Full Version : getting MySQL to work with Qt



Ashish
13th May 2009, 18:02
Hi,

I have been trying to get Qt to work with MySQL without any luck. When I run a 2 line test code, it comes back with a message saying- "This application has failed to start because LIBMYSQL.DLL was not found. Re-installing the application may fix this problem". Can anyone please guide me on fixing the issue and getting MySQL to work with Qt?

Here's what I have done so far-

1) I downloaded and installed MySQL 5.1.34 using the installer( mysql-5.1.34-win32.msi) from MySQL website.
2) I downloaded the latest opensource Qt sources(qt-win-opensource-src-4.5.1) from Qt website and configured it as follows:
a) added the path to locate qmake etc.
b). configure -debug-and-release -l libmysql -I D:\MySQL\MySQLServer5.1\include -L D:\MySQL\MySQLServer5.1\lib\opt -qt-sql-mysql -qt-sql-odbc
c) nmake

These steps went fine but when I run the test code, it doesn't work. The code that I was trying to run is :
------

#include <QCoreApplication>
#include <QSqlDatabase>
#include <QStringList>
#include <QtDebug>

int main( int argc, char **argv )
{
QCoreApplication app( argc, argv );
qDebug() << QSqlDatabase::drivers();
}

------

Can anyone please help me fix the issues here?

Thanks,
Ashish

wysota
14th May 2009, 01:16
Where did you place the MySQL dll? It should be in a folder that is in your system search path (i.e. C:\Windows\system32) so that the system dynamic linker can find it. You can also place it alongside your application binary as its directory is also searched by the linker.

Ashish
14th May 2009, 22:59
Hi Wysota,

Thanks for replying.

Are you talking about libmysql.dll? I cannot find any file by the name MYSQL.dll.

I did not place the 'libmysql.dll' anywhere, they are presently located in the MySql installation folder at "../MySQL/MySQLServer5.1/bin", "../MySQL/MySQLServer5.1/lib/debug" and "../MySQL/MySQLServer5.1/lib/opt"

Is there any other step that I need to do after the (1) Qt configure step as per my previous post and (2) nmake?

Thanks,
Ashish

wysota
15th May 2009, 00:09
They have to be accessible by the compiler and linker. Try placing the library in C:\Windows\System32 and see if it helps.

Ashish
15th May 2009, 15:59
Thanks Wysota. I placed the DLL library in my application directory and it works now.

The code from my first post works and gives me the following result:

("QMYSQL3", "QMYSQL", "QODBC3", "QODBC", "QSQLITE")
Press any key to continue . . .

However when I add a few more lines to my code to open a test database it doesn't work. Here is the new code:
-----

#include <QCoreApplication>
#include <QSqlDatabase>
#include <QStringList>
#include <QtDebug>
#include <QPushButton>
#include <QApplication>
#include <QSqlError>
#include <QSqlQuery>

#include <iostream>
using namespace std;
int main( int argc, char **argv )
{

QCoreApplication app( argc, argv );
qDebug() << QSqlDatabase::drivers();

QSqlDatabase *mydb = new QSqlDatabase();
mydb->addDatabase("QMYSQL");
mydb->setHostName("w.x.y.z"); //number changed
mydb->setPort(zzzz); //number changed
mydb->setDatabaseName("test");
mydb->setUserName("tmp");
mydb->setPassword("tmp123");
bool ok = mydb->open();
qDebug()<<mydb->lastError();
cout<<"ok="<<ok<<endl;

}
------

And here's the output of the above code:

("QMYSQL3", "QMYSQL", "QODBC3", "QODBC", "QSQLITE")
QSqlError(-1, "Driver not loaded", "Driver not loaded")
ok=0
Press any key to continue . . .

Can you please guide me on resolving the issue here? What am I doing wrong here?

Thanks,
Ashish

wysota
15th May 2009, 16:58
QSqlDatabase::addDatabase() is a static method.

MarkoSan
15th May 2009, 18:10
I've downloaded Qt 4.5.1 Opensource, Installed it (with mingw) and built mysql plugin according to Qt Centre's Wiki. Then I ran my app and I also get "Driver not loaded". Can someone help me here since I do not know what to do anymore ....

lyuts
19th May 2009, 08:57
I have a similar problem. I have built the PSQL driver and now it is the system's search path. But my application doesn't find this driver.