PDA

View Full Version : How to connect Qt with MYSQL??



Gokulnathvc
14th March 2011, 12:48
How to connect Qt with MYSQL??

mcosta
14th March 2011, 13:09
SeeQtSql and http://doc.trolltech.com/latest/sql-programming.html

Gokulnathvc
18th March 2011, 10:38
Anybody there to help me... Am using the following code. It is not connected..


QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("Qt");
db.setUserName("root");
db.setPassword("1");
bool ok=false;
ok=db.open();

mcosta
18th March 2011, 11:35
It is not connected.


this means that db.open() returns false?
The driver is available?

try with QSqlDatabase::isDriverAvailable


bool ok = QSqlDatabase::isDriverAvailable ("QMYSQL");

Gokulnathvc
18th March 2011, 12:22
Driver is not available... it returns false.. What should i do now??/

unit
18th March 2011, 12:56
Do you have compiled mysql plugin? Do you have libmysql ?

What OS are you have?

Gokulnathvc
18th March 2011, 13:01
windows xp. I didnt try with the mysql plugins.

unit
18th March 2011, 13:02
You need compile mysql plugin if you want use mysql

Do you read http://doc.trolltech.com/latest/sql-driver.html#supported-databases

Gokulnathvc
18th March 2011, 13:07
Actually libmysql file is not there in the Qt installed location

schnitzel
18th March 2011, 16:02
This has been discussed many times in this forum and the link from 'unit' contains all the information you need, please read it.

Also, remember that you need to install the MySQL development files (library and include files).
After building the plugin, the libs will be located in:
\Qt\<Qt version>\qt\plugins\sqldrivers

at least that's the location for Qt sdk on XP with mingw.

bmn
24th March 2011, 00:52
I don't know if the OP got this issue resolved, but I'd like to add an extra piece of info for anybody else struggling with this. Make sure that you either add the path to MySQL's lib directory to your PATH or drop the libmysql.dll into the same directory as your executable. I spent hours of compiling, recompiling, installing, and uninstalling stuff thinking I had done something wrong, just to find out the DLL dependency was my issue. Hope this saves somebody else the trouble.