PDA

View Full Version : Qt in windows and mysql !!!



probine
7th December 2006, 15:37
I am having trouble connecting from Qt (Windows) to my database.

I am using MySQL and the server is running. With some GUI clients I am connecting to the database, but when I try to connect from Qt, I just cannot.

I have included "#include <QtSql>" in my .cpp


QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("testdata");
db.setUserName("test");
db.setPassword("password");
bool ok = db.open();
if(ok)
textBrowser->append("Conection to DATABASE");
else
textBrowser->append("error to DATABASE");


I have also included "QT += sql" to my .pro

I can compile and run the code perfectly, there is no errors in it. But, but I guess that there is no connection, because I always end up printing the "error to Database" message on the screen.


Help please ?

probine
7th December 2006, 17:11
I have a simple code. The idea is just to connect to the MySQL database and print a message on the screen if the connection has been successful.

I have included in my "server.cpp" class this line:


#include <QtSql>


The code in the same class is:


QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("user");
db.setPassword("password");
bool ok = db.open();
if(ok)
textBrowser->append("Success");
else
textBrowser->append("Not database");


I am able to compile and also run this code. Their is no warning messages or anything like that, but it always prints "Not database" in the screen.

What do I need to do, to have MySQL and Qt in windows running ?

probine
7th December 2006, 18:36
I need to load the MySQL driver in Qt.

Windows XP
Qt 4.2.1
MySQL 5.0

This is the error message I get when trying to connect to the database:

"Driver not loaded Driver not loaded"

Please help !!!

Where can I find or download that driver ?

jacek
7th December 2006, 20:16
Please, don't start multiple threads on the same problem. Also search the forum, because this topic was discussed several times.

http://doc.trolltech.com/4.2/sql-driver.html#qmysql-for-mysql-4-and-higher

probine
7th December 2006, 22:22
I followed the instructions in how to built the plugin, but it doesn't work. I get this:

collect2: ld returned 1 exit status
mingw32-make[1]: *** [..\..\..\..\plugins\sqldrivers\qsqlmysql.dll] Error 1
mingw32-make[1]: Leaving directory `D:/qt-4.2.1/src/plugins/sqldrivers/mysql'
mingw32-make: *** [release] Error 2

probine
7th December 2006, 23:16
I have tried to create the dumm plug-in, but it doesn't work.

Is it there a tutorial where someone explain in detail what to do ?

jacek
7th December 2006, 23:29
I followed the instructions in how to built the plugin, but it doesn't work. I get this:

collect2: ld returned 1 exit status
mingw32-make[1]: *** [..\..\..\..\plugins\sqldrivers\qsqlmysql.dll] Error 1
mingw32-make[1]: Leaving directory `D:/qt-4.2.1/src/plugins/sqldrivers/mysql'
mingw32-make: *** [release] Error 2
The actual error message was a bit earlier. Maybe you don't have a MySQL client library that you can link to?

probine
8th December 2006, 10:08
What is wrong !!! What ????

Please someone help !!!

I have reinstalled everything, including Windows. I have nothing else in my computer than the following:

Qt 4.2.2 and the Mingw that comes with it.
MySQL server 5.0.27.

I have registered the variables in the "Path", so when I type "make" or "qmake" in the console, then those commands are executed.

Then I did this:

cd %QTDIR%\src\plugins\sqldrivers\mysql
qmake -o Makefile "INCLUDEPATH+=C:\MySQL\include" "LIBS+=C:\MySQL\lib\opt\libmysql.lib" mysql.pro
make

The "qmake" command is successful, but the "make" command gives me this error:

g++ -mthreads -Wl,-enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runt
ime-pseudo-reloc -Wl,-s -Wl,-s -shared -Wl,--out-implib,c:\qt\plugins\sqldrivers
\libqsqlmysql.a -o "..\..\..\..\plugins\sqldrivers\qsqlmysql.dll" tmp\obj\releas
e_shared\main.o tmp\obj\release_shared\qsql_mysql.o tmp\obj\release_shared\moc_q
sql_mysql.o -L"c:\qt\lib" -L"c:\qt\lib" C:\mysql\lib\opt\libmysql.lib -lQtSql4
-lQtCore4
Creating library file: c:\qt\plugins\sqldrivers\libqsqlmysql.a
tmp\obj\release_shared\qsql_mysql.o(.text+0x4c):qs ql_mysql.cpp: undefined refere
nce to `mysql_character_set_name@4'
tmp\obj\release_shared\qsql_mysql.o(.text+0x184):q sql_mysql.cpp: undefined refer
ence to `mysql_error@4'
tmp\obj\release_shared\qsql_mysql.o(.text+0x19a):q sql_mysql.cpp: undefined refer
ence to `mysql_errno@4'
tmp\obj\release_shared\qsql_mysql.o(.text+0x6d7):q sql_mysql.cpp: undefined refer
.
.
.
continues with a long list of errors until
.
.
.
tmp\obj\release_shared\qsql_mysql.o(.text+0x72ea): qsql_mysql.cpp: undefined refe
rence to `mysql_stmt_bind_param@8'
collect2: ld returned 1 exit status
make[1]: *** [..\..\..\..\plugins\sqldrivers\qsqlmysql.dll] Error 1
make[1]: Leaving directory `C:/qt/src/plugins/sqldrivers/mysql'
make: *** [release] Error 2

What in the world is wrong ?????

jacek
8th December 2006, 11:12
"LIBS+=C:\MySQL\lib\opt\libmysql.lib"
MinGW doesn't use .lib files. You have to create an import library from DLL.

http://mingw.org/mingwfaq.shtml#faq-msvcdll

probine
8th December 2006, 12:50
I just don't get it !!!

Jacek, isn't there something simple... a command you know, so I have to avoid all that trouble ?

I am sure there should be a simple way to do it, a linking command or something !, perhaps a .dll that I can download ?

jacek
8th December 2006, 13:41
a command you know, so I have to avoid all that trouble ?
The exact commands you should run are in that FAQ entry.

Also read this:
http://www.qtcentre.org/forum/f-qt-programming-2/t-connection-to-mysql-windows-4048.html

probine
8th December 2006, 14:57
Let me put it this way:

How do I configure Qt to work with MySQL ?

In Linux I could just type, I don't remember it exactly, something like:

./configure -static -plugin-sql-mysq -I /something/here/mysql.h -L /somathing/here/libmysqlclient

Afte "make" and the just mentioned command, I could use Qt with Mysql.

Now...now.... how is it done for Windows ? Should be as simple as that right ?

jacek
8th December 2006, 15:10
Should be as simple as that right ?
It is, but you have to prepare libmysql.a first --- read those two links I posted earlier and you'll know how to do it.

ksqt
9th December 2006, 08:42
http://www.qtcentre.org/forum/f-installation-5/t-mysql-plugin-1738.html/?highlight=mysql

Try that thread. It was the only way I got it to work.