PDA

View Full Version : Build Mysql plugin for Qt



Qt Coder
12th September 2009, 10:17
http://doc.trolltech.com/3.0/sql-driver.html#3-1

From above site I read

How to build the plugin on Windows

You need to get the MySQL installation files. Run SETUP.EXE and choose "Custom Install". Install the "Libs & Include Files" Module. Build the plugin as follows (here it is assumed that MySQL is installed in C:\MYSQL):

cd %QTDIR%\plugins\src\sqldrivers\mysql
qmake -o Makefile "INCLUDEPATH+=C:\MYSQL\INCLUDE" "LIBS+=C:\MYSQL\LIB\OPT\LIBMYSQL.LIB" mysql.pro
nmake

If you are not using a Microsoft compiler, replace nmake with make in the statement above.


my question is From where I will get MySQL installation files????????

faldzip
12th September 2009, 11:07
hmm... let me guess: http://www.mysql.com

Qt Coder
15th September 2009, 12:32
I configued Qt for Mysql using following instructons


Creating the MySQL client library

The client libraries (libmysql.dll and libmysql.lib) that are shipped with MySQL are compiled with the Microsoft compiler and cannot be linked using the MinGW compiler. Therefore, we first need to create a MinGW compatible library out of libmysql.dll.

This can be done from the command line as follows:

# cd c:\mysql\lib\opt
# reimp -d libmysql.lib
# dlltool -k --input-def libmysql.def --dllname libmysql.dll --output-lib libmysql.a

reimp is a tool to convert Microsoft's new-style (short) import libraries to be compatible with the Win32 ports of the GNU tools (MinGW, Cygwin). It reads the MS import library and writes all the library imports to a corresponding libmysql.def file used by dlltool to create the import library.

dlltool takes the libmysql.def file that was created by reimp, and creates the GNU compatible library libmysql.a.

Both these tools are found in mingw-utils.

Building the QMYSQL plugin dll

Build the Qt MySQL plugin based on the above MinGW compatible library using the following steps. This assumes that QT has been installed at C:\qt. Substitute the path that you have selected into the following commands. You need the MySQL C Include Files/Lib Files package for this step.

# cd c:\qt\src\plugins\sqldrivers\mysql
# qmake -o Makefile "INCLUDEPATH+=C:\MYSQL\INCLUDE" "LIBS+=-LC:\MYSQL\LIB\OPT -lmysql" mysql.pro
# mingw32-make

The files qsqlmysql.dll, qsqlmysqld.dll, libqsqlmysql.a, libqsqlmysqld.a will appear in your c:\qt\plugins\sqldrivers directory.

Make sure that libmysql.dll is in your path. This is located in the C:\mysql\bin directory which can be placed in the path environment variable and is provided by the MySQL client libraries (it is also included in the Include Files/Lib Files package in the full version of MySQL5).

I can see a libmysql.a and LIBMYSQL.def file created in C:\mysql\lib\opt\

then I copied libmySQL.dll from c:\mysql\bin to D:\Qt\4.4.3\bin and also copied this dll where my executable resides.

No when I query to fetch list of database drives I can see

QSQLITE
QMYSQL3
QMYSQL
QODBC3
QODBC

also I m able to connect to Mysql database on remot server


But the problem is that I m not able to fetch any records from the DB..

I use follwing code



QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");

db.setHostName("192.168.8.193");
db.setDatabaseName("gaur01");
db.setUserName("gaur");
db.setPassword("gaur123");
db.setPort(3306);


if (!db.open()) {
out <<"Error\n";
out << db.lastError().driverText();
out << "\n";
out << db.lastError().databaseText();
}
else
{
out <<"DB open successfully";
if(query.lastError().type() != QSqlError::NoError)
{

out << "\nError Occured 1 : " << query.lastError().text();
}

query.prepare("SELECT * FROM temp");
query.exec();

if(query.lastError().type() != QSqlError::NoError)
{
out << "\nError Occured 2 : " << query.lastError().text();
}


its output is


DB open successfully
Error Occured 1 : Driver not loaded Driver not loaded
Error Occured 2 : Driver not loaded Driver not loaded




any help ????

faldzip
15th September 2009, 15:28
can you check the returned value of query.exec() and try to read query results?

Qt Coder
16th September 2009, 06:58
query.exec();

is returning false

and when I fetch last error using


if(query.lastError().type() != QSqlError::NoError)
{
out << "\nError Occured 2 : " << query.lastError().text();
}


the output is

Error Occured 2 : Driver not loaded Driver not loaded



what could be the possible error?

Qt Coder
17th September 2009, 08:36
again I followed following instructions

################################3
How to Build the Plug-in

1. Open a Qt Command Prompt and go to wherever you installed the MySQL server (C:\Program Files\MySQL\MySQL Server 5.0 is the default location).

2. Goto the sub-directory lib/opt and run reimp libmysql.lib to produce the liblibmysql.a file. This is the import library to use with MinGW.

3. Go to %QTDIR%/src/plugins/sqldrivers/mysql.

4. Run the following command: qmake -o Makefile "INCLUDEPATH+=C:\Progra~1\MySQL\MySQLS~1.0\include" "LIBS+=C:\Progra~1\MySQL\MySQLS~1.0\lib\opt\liblibm ysql.a" mysql.pro (Notice that the Program Files directory name has been replaced by progra~1. This is because QMake cannot handle spaces in the search paths. Use dir /X <dirname> to find the shorter 8.3 version of the name.)

5. Run make - this should build the qsqlmysql.dll and libqsqlmysql.a files in the %QTDIR%/plugins/sqldrivers directory.

#########################

Till step 4 it runs successfully,but when I run make , after few hours of comiling it gives me error

"C:\MinGW\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\ mingw32\bin\ld.exe: cannot fin
d -lmysql"


last I could make it successfulyy but the files produced were "libqsqlmysql4.a" and "qsqlmysql4.dll"

Now its showing me QMYSQL3 and QMYSQL in list of availabel drivers but as I execute or prepare for query it gives error "Driver not loaded"

Qt Coder
18th September 2009, 07:57
My problem is now,


Its showing me MYSQL,MYSQL3 in the list of availabel drivers also allows to connect to database,but as I prepare any query or execute it it gives error "Driver not loaded"





strange behaviour......

faldzip
18th September 2009, 08:33
can you show the whole code? because I can't find the place where you create the query object.

Qt Coder
18th September 2009, 10:40
QSqlQuery query;

QFile file("d://DB_TRANS.txt");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);

QStringList list = QSqlDatabase::drivers();
QStringList::Iterator it = list.begin();
while( it != list.end() ) {
out << (*it) << "\n";
++it;
}

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");

db.setHostName("192.160.8.153");
db.setDatabaseName("gaur01");
db.setUserName("gaur");
db.setPassword("gaur123");
db.setPort(3306);

if (!db.open()) {
out <<"Error\n";
out << db.lastError().driverText();
out << "\n";
out << db.lastError().databaseText();
}
else
{
out <<"DB open successfully";
if(query.lastError().type() != QSqlError::NoError)
{

out << "\nError Occured 1 : " << query.lastError().text();
}

if(query.prepare("SELECT * FROM temp"))
out << "\nQuery prepared successfully : " ;
else
{
out << "\nError in preparing query: " << query.lastQuery();
out << query.lastError().text();
}

if(query.exec())
{
out << "\nQuery executed successfully : " ;
db.commit();
}


if(query.lastError().type() != QSqlError::NoError)
{

out << "\nError Occured 2 : " << query.lastError().text();
}

the output is

QMYSQL3
QMYSQL
QSQLITE
QODBC3
QODBC

DB open successfully

Error Occured 1 : Driver not loaded Driver not loaded

Error in preparing query: Driver not loaded Driver not loaded

Error Occured 2 : Driver not loaded Driver not loaded

faldzip
19th September 2009, 09:07
you are creating query (with a deafault database connection) before creating any databade connection. Try:

QSqlQuery query(db);
after creating db.