PDA

View Full Version : Qt and MySQL Database Connection



shamik
14th September 2006, 13:28
hiii,

Please help me connect Qt and MySQL in Linux(RH9). I am using Qt 3.3.0 and MySQL 5.0.0 alpha.

I am not able to do database connection in Qt with MySQL.

When I go to "project->database connections" the only driver that is listed is "QMYSQL3" and after providing the information in all the fields when i click "connect" then the whole window disappears and I have to start Qt again.

I have already installed all the drivers and compiled as is given in the manual.
but still cant connect.

Please help me build this database connection.

Thanks and Regards.
shamik

wysota
14th September 2006, 15:25
Qt3 doesn't support MySQL 5.0. About the crash - run QtDesigner under a debugger and see why it crashes. You might be missing some symbols if you don't have a proper mysql client library.

shamik
18th September 2006, 09:38
qt3 supports mysql5 and it has been done before by somebody and also has been confirmed by the trolltech support team.

i have compiled the drivers using following qmake command :

qmake -0 Makefile "INCLUDEPATH+=/usr/local/mysql/include" "LIBS+=-L/usr/local/mysql/lib" mysql.pro

make.

it compiled fine.

the driver QMYSQL3 is shown in the driver list in project->database connection.

i have written a small code to setup the database connection.

it also compiles well without error and exe is formed.

but when i try to run the exe from the shell it gives the following error :

[root@RedhatLinux database setup]# ./dbsetup
./dbsetup: relocation error: /usr/local/qt/plugins/sqldrivers/libqsqlmysql.so: undefined symbol: mysql_server_init
[root@RedhatLinux database setup]#

i dont know what to do know. i think all the libraries are included and all the paths are set correctly. the file libqsqlmysql.so also exists in the given folder.

please tell me whatz wrong here.

shamik

wysota
18th September 2006, 15:14
qt3 supports mysql5 and it has been done before by somebody and also has been confirmed by the trolltech support team.
Yes, but probably not out of the box.


i have compiled the drivers using following qmake command :

qmake -0 Makefile "INCLUDEPATH+=/usr/local/mysql/include" "LIBS+=-L/usr/local/mysql/lib" mysql.pro
Do you have /usr/local/mysql/lib in your linker path?


the driver QMYSQL3 is shown in the driver list in project->database connection.
Are you sure it is the same driver you compiled? Did you install the driver? Compiling it is not enough, it should be put in an appropriate place for Qt to find it.


i have written a small code to setup the database connection.
Can you present the code here?


but when i try to run the exe from the shell it gives the following error :

[root@RedhatLinux database setup]# ./dbsetup
./dbsetup: relocation error: /usr/local/qt/plugins/sqldrivers/libqsqlmysql.so: undefined symbol: mysql_server_init

Make sure the plugin tries to link against the proper mysqlclient library. You can do that using ldd.

shamik
20th September 2006, 14:56
hii,

thanks for the detailed information.

the code is here :

#include<qapplication.h>
#include<qsqldatabase.h>

int main(int argc, char **argv)
{
Qapplication a(argc,argv);
QsqlDatabase *defaultDB=QsqlDatabase::addDatabase(“QMYSQL 3”);
defaultDB->setDatabaseName(“manager”);
defaultDB->setPassword(“ “);
defaultDB->setHostName(“localhost”);

if(defaultDB->open())
{
qWarning(“Database connection successful”);
}

else
{
qWarning(“Database connection failed”);
}

return a.exec();

}

and yes, please tell what do u mean by
"Yes, but probably not out of the box."
"Do you have /usr/local/mysql/lib in your linker path?"

i have compiled the same driver.
and yes i have inquired about the plugin,

i think it requires, mysqlclient.so.15, as somebody else suggested me, but i am not sure. As against this i have got mysqlclient.so.10 which gets installed after i install mysql-devel package.

if this is the case then can you please tell me where can i get this *.*15 file from. how can i download it or the entire package which contains this file.

regards
shamik

wysota
20th September 2006, 15:25
and yes, please tell what do u mean by
"Yes, but probably not out of the box."
I mean that binary distributed qmysql3 plugins won't use the mysqlclient library from MySQL5. You have to recompile the driver from sources.


"Do you have /usr/local/mysql/lib in your linker path?"
I mean does your linker know to look for libraries in /usr/local/mysql/lib?


i think it requires, mysqlclient.so.15, as somebody else suggested me, but i am not sure.
That's very bad, as you ought to be sure. Use ldd on the driver to find out what it needs and what it finds.


As against this i have got mysqlclient.so.10 which gets installed after i install mysql-devel package.
And probably it gets linked with the driver instead of the version from MySQL5 causing relocation errors.


if this is the case then can you please tell me where can i get this *.*15 file from. how can i download it or the entire package which contains this file.
Install MySQL5 on your system. And make sure the dynamic linker can find it.

BTW. Does this thread have anything to do with Designer? Moving to Qt Programming. Or maybe it should end up in "Installation"?

shamik
22nd September 2006, 07:43
ya thanks

i know it has got nothing to do with designer, ill surely move it to installation, but before that tell me how to move this thread.

and yes i know dont know which is the exact driver, wheather it is mysqlclient.so.10 or ..15. the connection doesnt work with 10. but cant say wheather definitely it is 15. if you know then please tell me. i cant find any source to know this. also in mysql.com and other sites also i have searched for the same, but i dint even got ..15. :eek:

as i told you i have done a new fresh instalation, i am quite sure that the actual driver only gets linked. i have posted the qmake command which i use.
the errors all the remain same after the installation too.

and about installig mysql5, let me inform you that the project is already in use in "indian space research organization" and we are just adding a new feature to it. so my seniors are not convinced about changing any kind of specification in the softwares used to build up the project. thats the tragedy with me that even after such advanced version of qt, kdevelop, mysql and redhat, i have to still work on with the older ones.

can you tel me wheather i m using the correct driver or not. its mysqlclient.so.10. if not then please tell me which is the correct one. from where can i get it.

wysota
22nd September 2006, 10:42
i know it has got nothing to do with designer, ill surely move it to installation, but before that tell me how to move this thread.
You can't. I can and I already did.


and yes i know dont know which is the exact driver, wheather it is mysqlclient.so.10 or ..15. the connection doesnt work with 10. but cant say wheather definitely it is 15. if you know then please tell me. i cant find any source to know this. also in mysql.com and other sites also i have searched for the same, but i dint even got ..15. :eek:
I have already told you -- use ldd.


as i told you i have done a new fresh instalation, i am quite sure that the actual driver only gets linked.
Sure it gets linked, but against what?


i have posted the qmake command which i use.
the errors all the remain same after the installation too.
Because it has nothing to do with Qt, only with linker path.


can you tel me wheather i m using the correct driver or not. its mysqlclient.so.10. if not then please tell me which is the correct one. from where can i get it.

mysqlclient.10 is an old library, MySQL 3.3 I think...

I suggest you read a little about things such as dynamic linking, ldd, ldconfig, LD_LIBRARY_PATH and /etc/ld.so.conf.

shamik
25th September 2006, 08:52
hii there

now let me make somethings clear which i have come to know recently

all the installation cds which we use are made by those who have already made the software using these. those guys have now left the company and we have lost contact with them. we only have to add a new feature to the existing one. so compulsorily we have to use these softwares only. and since these have worked in the past it should work now also right?

what happened today is in one of the pcs the when i pressed 'connect' in 'project->database settings' the qt window dint disappear as it used to.
the settings under that are as follows
connection name : default
driver : QMYSQL3
database name : mysql
user : root
password :
host : localhost
port : default

and when press connect the following exclamation had come :

"
Could not connect to database.
Please ensure that the database server is running and that all the connection information is correct.
[QMYSQL3 : Unable to connect.
Cant connect to local MySQL server through socket 'tmp/mysql.sock' (2)]
"
the file mysql.sock is present in /var/lib/mysql dir. and not in /tmp
and now there are now problems in running mysql as we were facing previously. the only thing is that everytime i start mysql i have to run /etc/init.d/mysqld start .

and yes the mysql server is running when i see under server setting->services.

i have also tried to run the code which i have already posted.

when i try to run the exe from the shell it gives following error: (exe's name is driver)

QSqlDatabase Warning : QMySQL3 driver not loaded
QSqlDatabase : available drivers :
Database Connection failed (this is the message from our code)
[2]+stopped ./driver

so this all is new.

now lets go back again.
in my pc as you told to try out ldd
i tried it on libqsqlmysql.so which is in /usr/local/qt/plugins/sqldrivers
it listed the library file of mysql called libmysqlclient.so.10 along with many others.

when i try to run nm on this file which is in /usr/lib/mysql/ it says no symbols found
again on the file libqsqlmysql.so i had tried out nm and it shows that mysql_server_init is undefined.

now i am quite sure that the file ..10 is the correct driver and not ..15 because this has been done before by somebody.

what now!!?!?

shamik

wysota
25th September 2006, 09:08
i tried it on libqsqlmysql.so which is in /usr/local/qt/plugins/sqldrivers
it listed the library file of mysql called libmysqlclient.so.10 along with many others.
So it links against MySQL3 and not MySQL5 client library.


when i try to run nm on this file which is in /usr/lib/mysql/ it says no symbols found
That's because the library is stripped, don't worry, that's perfectly normal.


again on the file libqsqlmysql.so i had tried out nm and it shows that mysql_server_init is undefined.
That's normal too, because it will be defined when dynamic loader links the driver against mysql library.


now i am quite sure that the file ..10 is the correct driver and not ..15 because this has been done before by somebody.
So you need MySQL3 or MySQL5? Do you have libmysqlclient.so.10? (locate libmysqlclient.so should help find it)


what now!!?!?

Does ldd report any missing paths? (try ldd libqsqlmysql.so|grep "not found" or something simmilar)

From the message you provided I can see that the driver plugin can't be loaded. It is either not found at all (where is the file located?) or it can't find one of its dependencies (ldd should verify that).

shamik
25th September 2006, 11:03
So it links against MySQL3 and not MySQL5 client library.


no, but i told u that it should work against mysql5 also as it has been done before.


So you need MySQL3 or MySQL5? Do you have libmysqlclient.so.10? (locate libmysqlclient.so should help find it)

ya i have libmysqlclient.so.10 and it is /usr/lib/mysql directory.

Does ldd report any missing paths? (try ldd libqsqlmysql.so|grep "not found" or something simmilar)

this is the result of ldd:

[root@localhost sqldrivers]# ldd libqsqlmysql.so
libmysqlclient.so.10 => /usr/lib/mysql/libmysqlclient.so.10 (0x40023000)
libqt.so.3 => /usr/local/qt/lib/libqt.so.3 (0x4005a000)
libXrender.so.1 => /usr/X11R6/lib/libXrender.so.1 (0x4081e000)
libXrandr.so.2 => /usr/X11R6/lib/libXrandr.so.2 (0x40826000)
libXcursor.so.1 => /usr/X11R6/lib/libXcursor.so.1 (0x4082b000)
libXft.so.2 => /usr/X11R6/lib/libXft.so.2 (0x40834000)
libfreetype.so.6 => /usr/lib/libfreetype.so.6 (0x40846000)
libfontconfig.so.1 => /usr/lib/libfontconfig.so.1 (0x40897000)
libXext.so.6 => /usr/X11R6/lib/libXext.so.6 (0x408bc000)
libX11.so.6 => /usr/X11R6/lib/libX11.so.6 (0x408ca000)
libSM.so.6 => /usr/X11R6/lib/libSM.so.6 (0x409aa000)
libICE.so.6 => /usr/X11R6/lib/libICE.so.6 (0x409b3000)
libdl.so.2 => /lib/libdl.so.2 (0x409ca000)
libstdc++.so.5 => /usr/lib/libstdc++.so.5 (0x409cd000)
libm.so.6 => /lib/tls/libm.so.6 (0x40a80000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x40aa2000)
libc.so.6 => /lib/tls/libc.so.6 (0x42000000)
libz.so.1 => /usr/lib/libz.so.1 (0x40aab000)
libcrypt.so.1 => /lib/libcrypt.so.1 (0x40ab9000)
libnsl.so.1 => /lib/libnsl.so.1 (0x40ae6000)
libexpat.so.0 => /usr/lib/libexpat.so.0 (0x40afb000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)
[root@localhost sqldrivers]#ldd libqsqlmysql.so | grep "not found"
[root@localhost sqldrivers]#



From the message you provided I can see that the driver plugin can't be loaded. It is either not found at all (where is the file located?) or it can't find one of its dependencies (ldd should verify that).

so as ldd doesnt report anything missing , everything else is fine.
i think i am just little bit far from the end.

wysota
25th September 2006, 11:26
Where is libqsqlmysql.so and what paths are mentioned by qtconfig as library paths?

shamik
25th September 2006, 12:06
libqsqlmysql.so is in /usr/local/qt/plugins/sqldrivers

there are two paths mentioned under library paths tab when i type ./qtconfig they are

/usr/local/qt/plugins
/usr/local/qt

wysota
25th September 2006, 12:19
Seems fine... And what does QSqlDatabase::drivers() return?

One more thing I just thought of... Are you sure you're not trying to access an embedded mysql database using a client only library? mysql_server_init() symbol name suggests trying to use an embedded database, which is available in libmysqld.so not in libmysqlclient.so. Did you compile the qsqlmysql driver yourself? What switches did you pass during compile process?

shamik
25th September 2006, 15:15
no i think i am not trying to access an embedded mysql database.

and i have tried alot but i am not able to write a code to print the list of drivers using QSqlDatabase::drivers()

actually i am able to store the list, this function returns but i dont know how to print that list.

i think QMYSQL3 might be the only driver since only this is listed under project->database settings->driver

what say!!:eek:

wysota
25th September 2006, 18:11
no i think i am not trying to access an embedded mysql database.
You think that or you verified that?


and i have tried alot but i am not able to write a code to print the list of drivers using QSqlDatabase::drivers()
actually i am able to store the list, this function returns but i dont know how to print that list.

QStringList slist = QSqlDatabase::drivers();
for(int i=0;i<slist.size();i++) qDebug("%s", slist[i].ascii());


i think QMYSQL3 might be the only driver since only this is listed under project->database settings->driver

It's not a matter of thinking, it's a matter of checking different possibilities to find out what the problem is.

What about answers to my other questions?

shamik
26th September 2006, 10:40
ok
the code gives the list -- QMYSQL3
and i have to stop the program manually by pressing ctrl-Z

ofcourse i have compiled the driver myself using the qmake which i have submitted.
and m sorry i dont know what are switches.

and can you tell me (going back to our previous prob) why does the qt crashes when i press connect. i dont know how to open and run it under any debugger. i have tried using the debugger present in programming-debugger and failed miserably.

moreover in other pc as the problem i have told you about, it gives the error that it cant connect through socket /tmp/mysql.sock. this file is not present there but exists in /var/lib/mysql/

how to tell qt to change the path into which it is looking. i have added a library path -- /var/lib/mysql in qtconfig. but can see no difference.

wysota
26th September 2006, 10:57
using the qmake which i have submitted.
What do you mean by that?


and m sorry i dont know what are switches.
Commandline parameters passed to configure.


and can you tell me (going back to our previous prob) why does the qt crashes when i press connect.
You mean the relocation error? It is because when a plugin is loaded, the dynamic linker has to resolve all the symbols from the plugin and its dependencies. And it fails to do so.


i dont know how to open and run it under any debugger. i have tried using the debugger present in programming-debugger and failed miserably.
If it is a relocation error, then the debugger won't help you. But if you want to check other things, then try this:
$ gdb ./your_application
(gdb) run
(...)
(gdb) bt

This will give you the backtrace -- a list of calls which led to the error.


moreover in other pc as the problem i have told you about, it gives the error that it cant connect through socket /tmp/mysql.sock. this file is not present there but exists in /var/lib/mysql/
You have to instruct MySQL to put it in /tmp or convince the driver (using my.cnf I think) to look for it elsewhere.


how to tell qt to change the path into which it is looking. i have added a library path -- /var/lib/mysql in qtconfig. but can see no difference.
It is MySQL which fails, not Qt.

shamik
26th September 2006, 12:54
hiii there

thanks alot for the help
finally i have done the connection (in other pc, though not in mine). the very small thing took a hell lot of time.
thanks alot for that.

what i did is just changed the path in my.cnf to /tmp

but wait a min. in my pc the error is same old one and got to solve it.
about the switches you told , i dont pass anything in that. what i do is simply type:
./configure

and then

make.

then i install the mysql-devel package and then run qmake as under :
1) qmake -o Makefile "INCLUDEPATH+=/usr/local/mysql/include" "LIBS+=-L/usr/lib/mysql" mysql.pro

2) qmake -o Makefile "INCLUDEPATH+=/usr/local/mysql/include" "LIBS+=-L/usr/local/mysql/lib" mysql.pro

3) qmake -o Makefile "INCLUDEPATH+=/usr/local/mysql/include" "LIBS+=-L/usr/local/mysq/lib -lmysqlclient" mysql.pro

i have tried all the three of these, but none works.
i am planning to install qt again in my pc and start from scratch. i usually follow the instructions given in the "install" file in the qt folder.

can u tell me the step by step process of qt installation if anywhere i am going wrong?

and yes one more thing. we already had qt 3.1.1 installed with linux but we have installed qt3.3.0.
now we want to integrate it with kdevelop. when we click the qt icon given in the kdevelop gui the new version i.e. qt3.3.0 opens up. thats fine and as we wanted.

but when we try to add new qt ui file to new project or existing project by going into
file->new->qtdesigner(*.ui), give the file name and then click ok then the older qt3.1.1 gets open. i want that new qt should get opened. do you know anything about this.

if possible then tell me otherwise our previous problem is more important.
and i think we are about to get it done.

thanks again

wysota
27th September 2006, 11:55
Install mysql development packages before compiling Qt. Otherwise it won't know that it is to compile the plugin at all or will link it against some other mysql library. Also make sure that mysql installation you performed is seen "by default" by the system. Otherwise you might need to pass some switches to configure (see configure -help for details).

shamik
27th September 2006, 12:06
what are these mysql development packages. and tell me how does mysql installation is seen bydefault by the system. what actually d u mean by this. i had tried configuring qt many times as under:
./configure -qt-sql-mysql
./configure -plugin-sql-mysql

but everytime it gave some error during make process. in my pc and other ones too, qt installation takes more than 4hrs of time.

i have already started qt installation and by evening it will get over.
before that i had re-installed mysql but after the installation all the tables, and databases which i had made previously existed in the newer installation also which i think should have got vanished.:confused:

i am again planning to do linux installation again and then mysql and then qt :(

please help me yaar, i am fed up of this stupid things. why does it work in one of the pcs and why not in mine?

wysota
27th September 2006, 14:08
what are these mysql development packages.
mysql-devel or simmilar.


and tell me how does mysql installation is seen bydefault by the system. what actually d u mean by this.
I'm sorry, this is a Qt related forum. I don't feel competent enough to answer MySQL related questions. By "seen by default" I meant that the library and header files is visible by the loader/compiler without need to set any environment variables or something like that.


i had tried configuring qt many times as under:
./configure -qt-sql-mysql
./configure -plugin-sql-mysql
It is possible that you should also use -I and -L to point the compile process to appropriate places of your MySQL installation. This is if your MySQL installation is not "seen by default" by your system.


but everytime it gave some error during make process.
If you report the errors here, maybe we'll be able to help solve them. Alternatively use Google and solve them yourself.


in my pc and other ones too, qt installation takes more than 4hrs of time.
That's normal for Qt3. My first own compilation of Qt3 took about 8 hours.



before that i had re-installed mysql but after the installation all the tables, and databases which i had made previously existed in the newer installation also which i think should have got vanished.:confused:
Did you do that using some package manager? If so, they should be safe.


i am again planning to do linux installation again and then mysql and then qt :(
It's not Windows, you don't need to reinstall everything just to get one component to work.


please help me yaar, i am fed up of this stupid things. why does it work in one of the pcs and why not in mine?
You probably use some custom things. Trying to get MySql5 work with qmysql3 is one of them, IMO...

shamik
27th September 2006, 14:30
hiii

about the installation , as u were telling it takes around 8hrs wth u, but at my friend's home it takes just 20 mins, he's got fedora 5 but qt version is same. has it got anything to do with processor??

i dint un/installed mysql using any package manager. what i did is just deleted the mysql folder from /usr/local and then again re-installed from the .tar file i had to the same folder /usr/local/mysql.

i dont really setup any environment variables during mysql installation. only that my.cnf gets modified to set up the path of mysql.sock file. nothing else

now what?

wysota
27th September 2006, 16:34
has it got anything to do with processor??
Sure, the faster the processor, the shorter the compilation time.


i dint un/installed mysql using any package manager. what i did is just deleted the mysql folder from /usr/local and then again re-installed from the .tar file i had to the same folder /usr/local/mysql.
So you deleted all your databases with it. If you use RedHat (and I think you do), use its package manager (Yast or something like that) to install the software from RPMs. You can install appropriate RPMs manually too. This will save you a lot of trouble.


i dont really setup any environment variables during mysql installation. only that my.cnf gets modified to set up the path of mysql.sock file. nothing else
If you install any software in a custom place (and /usr/local/mysql might be considered one), you should inform your system, that the software is there and that it should look for it there. Otherwise it will be unable to use it.

shamik
28th September 2006, 07:51
i have mysql 5 as .tar file so i dont use rpm package manager to install that. and since it doesnt list mysql5 in its package's list i dnt use it either to uninstall also. when i give the command : rpm -qa | grep mysql all it lists is :
mysql-server 3.23.. (..version..)
mysql 3.23
mysql-devel 3.23

and not mysql5.

i have installed mysql in a custom place /usr/local/mysql. tell me how to inform the system about it.
will that solve the relocation error it is giving?

wysota
28th September 2006, 08:15
http://unixhelp.ed.ac.uk/CGI/man-cgi?ld.so
http://unixhelp.ed.ac.uk/CGI/man-cgi?ldconfig+8

I don't know if it will solve them, but you surely won't be able to use mysql5 client library without it. Also make sure you compile the qsqlmysql driver against that library.

shamik
28th September 2006, 11:26
thanks for the links you provided.

as u suggested i have tried out
$gdb ./designer

i.e. opened qt in debugger. then i tried to make the connection by selecting the QMYSQL3 driver and the clicked connect. as usual qt crashed. but on the shell i got following error:

#$gdb ./designer

QSqlDatabase : driver not loaded
QSqlDatabase : available drivers : QMYSQL3
./designer : relocation error : /usr/local/qt/plugins/sqdrivers/libqsqlmysql.so : undefined symbol : mysql_server_init

from the links you provided i tried out
ldconfig

it did something, (i donno what) and then returned to the shell.

then i tried
ld libqsqlmysql.so
it gives

ld : warning : cannot find entry symbol _start;not setting start address
libqsqlmysql.so : undefined reference to 'mysql_server_init'
libqsqlmysql.so : undefined reference to 'mysql_server_end'

and yes i have tried out what you told about linking to embedded mysql. but i dont have that mysqld.so library with me so i think it would not be the case. of that kind what i only have is mysqld.a file. all the .so files have the initials 'libmysqlclient'.

i think now it would help you to trace out the problem.

wysota
28th September 2006, 11:37
Did you add a path to your MySQL5 client library to ld.so.conf before running ldconfig? Did you recompile the driver afterwards?

Maybe you should first try to get QMYSQL3 with MySQL3 up and running? You might be chasing a bug which is caused by something completely different.

shamik
28th September 2006, 12:04
i have added the newer qt's library path to ld.so.conf in /etc . the contents of this file are now:

/usr/kerberos/lib
/usr/X11R6/lib
/usr/lib/sane
/usr/local/qt/lib
/usr/lib/mysql

the last two lines i have added. then i did ldconfig (without any options).
then i recomipiled my driver using the qmake and make process as i told u.
and then tested by $gdb ./designer

but everything is same. mainly the error.
actually i did ldconfig -v so that it would list the files it is processing. it showed libmysqlclient.so file in the /lib/mysql folder and the qt lib files also.

"Maybe you should first try to get QMYSQL3 with MySQL3 up and running"

what do you mean by this. i have already QMYSQL3 running on this particular MYSQL5 on one of the pcs and the connection is working fine. but on my pc only it is giving error. so i dont think it is a compatiblity issue.

shamik
29th September 2006, 11:55
hii there

i have installed linux, mysql and qt again. as u suggested i installed mysql devel package before installing qt.

everythings working fine. qt which used to crash before is not crashing anymore and the connection is seen and setup fine.

but one more problem has occured.

as i press connect the connection appears on the left side pane. that means the connection is setup. now i have tried to do a simple book's example from the qt manual.

in my mysql i have created a database called "manager" and inside it created a table called "book" and have some values inserted in it.

now (going back to qt) after creating the connections, i clicked on tools-database-datatable.
a data table setup appeared and i did as given in the manual. note that all the columns in the "book" table in mysql were shown after i finished the datatable settings. this means the driver is working fine and all the settings have been correct.

now i had pasted the following code from the manual to my project :


#include<qsqldatabase.h>
#include<qapplication.h>

bool createconnections()
{
QSqlDatabase *db= QSqlDatabase::addDatabase(“QMYSQL3”);
if(!db)
{
qWarning(“Failed to connect to driver”);
return FALSE;
}

db->setDatabaseName(“manager”);
db->setUserName(“root”);
db->setPassword(“”);
db->setHostName(“localhost”);

if(!db->open())
{
qWarning(“Failed to open books database : “ + db->lastError().driverText());
qWarning(db->lastError().databaseText());
return FALSE;
}
return TRUE;
}

int main(int argc, char *argv[])
{
Qapplication a(argc,argv);
If(!createconnections())
{
return 1;
}
BookForm bookForm;
a.setMainWidget(&bookForm);
bookForm.show();
return a.exec();
}

please ignore any spelling mistakes or caps mistake in the code as i have typed this manually in windows.

this code compiles well and exe is also formed as usual.

but when i run the exe from the shell it gives following error :

#./book
QSqlDatabase warning : QMYSQL3 driver not loaded
QSqlDatabase : available drivers :
Failed to open books database : Driver not loaded
Driver not loaded


please tell me where am i going wrong.

wysota
29th September 2006, 13:57
Do Qt SQL examples work correctly?

shamik
3rd October 2006, 06:37
sorry, it was 3 days holiday in our country (festivals)

anyways
i dont understand what u mean by "Do Qt SQL examples work correctly?"

the examples are given in the qt manual so i assumed they must be correct.
else can u provide me with a small code so that i can check wheather my connection is working or not.

thanks.

wysota
3rd October 2006, 07:26
i dont understand what u mean by "Do Qt SQL examples work correctly?"

the examples are given in the qt manual so i assumed they must be correct.
I'm not asking about the code but actual programs residing in examples/ and demos/ directories of your Qt application.


else can u provide me with a small code so that i can check wheather my connection is working or not.
I can't. You have Qt examples for that, that's why I asked you to check if they work :) I can provide code which is not bound to be correct, whereas Qt examples are correct for sure so everyone checks against them if they can.

shamik
3rd October 2006, 08:41
ok

the in the other pc, the code which i had provided is working and the exe is running good and shows all the contents of the database.

in my pc, i have tried the same code.
in the qt window when i press preview->preview form

the form with the datatable is shown and all the contents of the actual database in mysql are reflected in the datatable.
but why only when i run the exe nothing is shown. this means there is no problem with the driver as it showing the contents of the database.
there must be something else. what say??
also i have to open the newer version of qt manually.
the kdevelop doesnt take it bydefault. it takes the older version.
is there any setting by which kdevelop can take it automatically.

thanks
shamik

wysota
3rd October 2006, 10:46
ok

the in the other pc, the code which i had provided is working and the exe is running good and shows all the contents of the database.

in my pc, i have tried the same code.
in the qt window when i press preview->preview form

the form with the datatable is shown and all the contents of the actual database in mysql are reflected in the datatable.
but why only when i run the exe nothing is shown. this means there is no problem with the driver as it showing the contents of the database.
there must be something else. what say??
Please run the SQL examples from Qt3. Don't test your code as it may not be completely valid. Test on something which is known to be valid.


also i have to open the newer version of qt manually.
the kdevelop doesnt take it bydefault. it takes the older version.
is there any setting by which kdevelop can take it automatically.
I don't know what "newer" means in this case. Maybe you didn't install Qt correctly. If you install it in a custom path, you have to make sure $QTDIR and $PATH point to proper places. QTDIR should point to the main directory of Qt3 installation and PATH should contain the place where qmake, uic, moc and other needed binaries are stored (most probably $QTDIR/bin).

shamik
4th October 2006, 11:50
ok
now here is something new.

i have made a new project called book.pro
following is the code for it :


bool createConnections()
{
// create the default database connection
QSqlDatabase *defaultDB = QSqlDatabase::addDatabase( "QMYSQL3" );
if ( ! defaultDB )
{
qWarning( "Failed to connect to driver" );
return FALSE;
}
defaultDB->setDatabaseName( "mysql" );
defaultDB->setUserName( "root" );
defaultDB->setPassword( "" );
defaultDB->setHostName( "localhost" );
if ( ! defaultDB->open() ) {
qWarning( "Failed to open mysql database: " +
defaultDB->lastError().driverText() );
qWarning( defaultDB->lastError().databaseText() );
return FALSE;
}
return TRUE;
}
int main( int argc, char **argv )
{
QApplication a( argc, argv );
QDataTable tb(0,"Author Table");
BookForm w;
int i=0;
if ( ! createConnections() )
{
i=1;
return 1;
qWarning("This is my warning. Error is in createConnection(). i=%d",i);
}
// w.show();
// tb.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}

i have included following header files :
qapplication.h , qsqldatabase.h , qsqlrecord.h , qsqlfield.h , qdatatable.h , "form1.h"

as usual the exe is formed without any errors. u can try it if you want.
in the form i have a qdatatable.
now in mysql bydefault there is a database called "mysql"
i have tried to use that.
while creating qdatatable all the tables in the database "mysql" were listed and i have selected "help_category" table.
inside this table there are various entries already present.

when i preview the form , all the entries are listed as it is.

but when i run the program from the shell the following error comes :


[root@localhost book]# ./book
QSqlCursor::setName: unable to build record, does 'help_category' exist?
QSqlRecord::field: index out of range: 0
QSqlRecord::position: unable to find field name
QSqlRecord::field: index out of range: -1
QSqlRecord::field: index out of range: 1
QSqlRecord::position: unable to find field parent_category_id
QSqlRecord::field: index out of range: -1
QSqlRecord::field: index out of range: 2
QSqlRecord::position: unable to find field url
QSqlRecord::field: index out of range: -1
QSqlRecord::field: index out of range: 3
QSqlRecord::position: unable to find field help_category_id
QSqlRecord::field: index out of range: -1
QSqlRecord::field: index out of range: 0
QSqlRecord::position: unable to find field name
QSqlRecord::field: index out of range: -1
QSqlRecord::field: index out of range: 1
QSqlRecord::position: unable to find field parent_category_id
QSqlRecord::field: index out of range: -1
QSqlRecord::field: index out of range: 2
QSqlRecord::position: unable to find field url
QSqlRecord::field: index out of range: -1
QSqlRecord::field: index out of range: 3
QSqlRecord::position: unable to find field help_category_id
QSqlRecord::field: index out of range: -1
QSqlDatabase warning: QMYSQL3 driver not loaded
QSqlDatabase: available drivers:
Failed to open mysql database: Driver not loaded
Driver not loaded
[root@localhost book]#


i dont know what is wrong here.
pleeese check the code. i think it is not able to access the database. but as i can see all the fields of mysql when i preview the form, so i think there is no problem with the driver.

please do something. one problem gone, another came. i m frustrated now!!:confused:

this really sucks

wysota
4th October 2006, 12:00
First thing - please embed the source code into code tags. I have to correct that every time you post any source code, I think it's high time you started using the forum properly.

Second thing - why are you running the application as root?

Third thing - you have to create the connection before creating the data table.

Fourth thing - the driver is still not loaded. Why don't you run the examples as I asked you to?

shamik
4th October 2006, 13:46
yes brother

you are right

as i was compiling one of the examples i noticed that the library it was using was /usr/lib/qt-3.1

when i typed 'set' command from the shell the QTDIR path was not set to /usr/local/qt
where i have installed my new qt 3.3.0.

so i had to type the following on the shell
export QTDIR=/usr/local/qt
export PATH=/usr/local/qt:$PATH

and then when i typed the set command again the qtdir path was properly set.

but now whenever i try to compile and form the exe using 'make' command after doing qmake, everytime it gives the following error:
/usr/bin/ld : cannot find -lqt
collect 2 : ld returned 1 exit status
make : *** [sample]E\ Error 1

this error is same for all the programs(projects). so i think i have to set some other paths also.

whats that??

wysota
4th October 2006, 17:53
The projects are looking for singlethreaded Qt library (libqt) instead of multithreaded one (libqt-mt). You either have to correct the project files to include CONFIG+=thread (better option) or make a softlink from libqt-mt.so to libqt.so (worse option).

shamik
5th October 2006, 07:21
i have soved the previous problem myself. i dont know wheather what i did is right or not.
this is what i did .
i modified the ld.so.conf and properly set the qtdir path.
then i ran 'ldconf' command from shell. then qtdir path was set properly. then i recompiled the driver and that error of 'cannot find -lqt' has gone and the programs are now compiling and exe is also formed.
but the error is the same as i have posted earlier. it comes with two simultneous exclamations saying: "An error occured while accessing the database"
and then followed by that series of QSqlRecord.... (just see the previous to previous post)

you were talking of running the examples for qt3 in some of ur posts. what is that.

also one more thing i noticed.
everytime i restart my pc and type 'set' on the shell the qtdir path which i had set recently i.e. to /usr/local/qt again becomes the previous one which is /usr/lib/qt-3.1
and every time i have to export both QTDIR and PATH. is there any way that i dont have to it everytime i restart.

one more thing.
my-sql devel package has installed( i think) the files i.e. mysqlclient.so and all similar ones in folder /usr/lib/mysql

now notice this :
when i compile the driver with the qmake command as follows :
qmake -o Makefile "INCLUDEPATH+=/usr/local/mysql/include" "LIBS+=-L /usr/lib/mysql -lmysqlclient" mysql.pro

i.e. i give the library path of /usr/lib/mysql
the driver is formed. ok. but qt as usual crashed as our previous problem and all the programs gives relocation error as previously.
when i run ldd on that qsqlmysql.so (driver) then one of the listed file is libmysqlclient.so.10 in /usr/lib/mysql
fine.
but when i compile the driver with qmake command as follows :
qmake -o Makefile "INCLUDEPATH+=/usr/local/mysql/include" "LIBS+=-L /usr/local/mysql/lib -lmysqlclient" mysql.pro
i.e library path is now /usr/local/mysql/lib
the driver is formed. ok. the qt doesnt crash. ok. the connection is also formed. ok. and whenever i preview the form the database contents are also reflected in the datatable.
only that the programs are not running as the present case.
but when i run ldd on this particular 'libqsqlmysql.so' then none of the mysqlclient files are shown. i think it doesnt have any dependencies on mysqlclient libraries. thats the reason it might be having problems accessing database.

this is not as complicated as it seems coz just to give u whole explanation in detail i am writing so long.

my question is :
1). i have to set qtdir path everytime i restart. how to avoid it?
2). as u were telling, how to run examples as qt3? the older version on my system is qt-3.1 and the new is qt-3.3.0.
3). if the preview form can show the database contents then why is the error coming of accessing the database when i run the exe?
4). in ur previous post u were telling about single and multi thread. how to set those better and worse options?

this post is too long but it will give you the entire picture of the problem!!

wysota
5th October 2006, 13:07
i have soved the previous problem myself. i dont know wheather what i did is right or not.
this is what i did .
i modified the ld.so.conf and properly set the qtdir path.
then i ran 'ldconf' command from shell.

Very good.


but the error is the same as i have posted earlier. it comes with two simultneous exclamations saying: "An error occured while accessing the database"
and then followed by that series of QSqlRecord.... (just see the previous to previous post)
Did you make sure the connection is made before the datatable is created?


one more thing.
my-sql devel package has installed( i think) the files i.e. mysqlclient.so and all similar ones in folder /usr/lib/mysql

now notice this :
when i compile the driver with the qmake command as follows :
qmake -o Makefile "INCLUDEPATH+=/usr/local/mysql/include" "LIBS+=-L /usr/lib/mysql -lmysqlclient" mysql.pro

i.e. i give the library path of /usr/lib/mysql
the driver is formed. ok. but qt as usual crashed as our previous problem and all the programs gives relocation error as previously.
Because it's not enough to compile the driver against the proper library. The linker must be able to find the library when running the application. I already gave you some info about it earlier in this thread.


1). i have to set qtdir path everytime i restart. how to avoid it?
Add it to your profile (for example in /etc/profile.d/).


2). as u were telling, how to run examples as qt3? the older version on my system is qt-3.1 and the new is qt-3.3.0.
Well... cd to the directory where examples are ($QTDIR/examples maybe?), compile them and run them.

3). if the preview form can show the database contents then why is the error coming of accessing the database when i run the exe?
I don't know. Running the examples is to verify that.

4). in ur previous post u were telling about single and multi thread. how to set those better and worse options?
I'm sorry, this is a Qt forum, not a Linux-system-usage-howto one. Refer to your system manual for details on creating softlinks.

The bottom line is - I can't guide you through a custom installation of MySQL on your system. I advise you to find proper RPMs to install the dbms using regular methods which will prepare your system to using the library. If you persist on doing a custom installation - you have to do it on your own (or refer to a mysql-related site).

shamik
6th October 2006, 13:48
hiii there

i have solved all the problems.

now everything is working fine.

the reason why it was showing the database contents in preview and not while running the exe is that i had to change the place where i used to make object of the form.

first i used to make it in the begenning of the main function. but after i changed its position, i.e. i now make the object at the end of the main function after writing all the code and before calling that object.

it seems everything willl b alright now.
i'll mail you if i have some other problems in qt programming.

but thanks very very very much for helping me so far and taking interest in my problems.

thumbs up to u.

regards
shamik