PDA

View Full Version : Connectingto MySQL with QODBC3



Philip_Anselmo
6th June 2006, 23:39
Hi I'm trying to connect to a MySQL 3.23 on a Fedora Core 1 with an app I made on Qt 3.1.2
I installed the QODBC3 plugin through yum

yum install qt-ODBC
it installed also the Unix-ODBC driver

so I connect to MySQL server as this:



QSqlDatabase *defaultDB;
bool creaConexion()
{

defaultDB = QSqlDatabase::addDatabase( DBDRIVER );
defaultDB->setHostName( DB_HOSTNAME );
defaultDB->setDatabaseName( "DRIVER={MySQL};Server="+DB_HOSTNAME+";Database="+DB_NOM+";Uid= "+DB_USR+";Pwd="+DB_PASS+";" );
defaultDB->setUserName( DB_USR );
defaultDB->setPassword( DB_PASS );

if ( ! defaultDB->open() ) {
QMessageBox::warning(0, "Fallo de conexión", "No se pudo conectar con la base de datos: " + DB_NOM + "tabla: " + DB_TABLA + "\n" + defaultDB->lastError().driverText());
return FALSE;
}
QMessageBox::information(0,"Conexión estabecida","Conectado a: " + DB_HOSTNAME + " " + "tabla: " + DB_TABLA + " "+ DB_NOM);
return TRUE;
}


I don't see what's wrong so I ask you for help :o

I'm geting this error:



Warning: QSqlDatabaseManager unable to open database: [unixODBC][Driver Manager]Data source name not found, and no default driver specified : QODBC3: Unable to connect

wysota
7th June 2006, 00:29
You have to setup the database in Unix-ODBC. Unfortunately I know nothing about it :) Maybe mr Google (http://www.google.com) does.

Philip_Anselmo
7th June 2006, 17:50
I checked the driver configuration in odbc

the /etc/odbcnist.ini looks like this:



# Example driver definitinions
#
#

# Included in the unixODBC package
[PostgreSQL]
Description = ODBC for PostgreSQL
Driver = /usr/lib/libodbcpsql.so
Setup = /usr/lib/libodbcpsqlS.so
FileUsage = 1


# Driver from the MyODBC package
# Setup from the unixODBC package
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/libmyodbc.so
Setup = /usr/lib/libodbcmyS.so
FileUsage = 1



the only problem is that I can't find /usr/lib/libmyodbc.so
that file is missing on this fedora core 1 wich uses unixODBC 2.2.8 and I can't install other version because of the dependencies and I just can't find the 2.2.8 version on the net :crying:

I just can't work like that :(
How can I get that damn .so file?

Philip_Anselmo
7th June 2006, 18:00
jjaja nevermind I just founded the myodbc on the fedora website :P

but now it throwsme a segment violation :S:S:S

is this code right?:



QSqlDatabase *defaultDB;
bool creaConexion()
{

defaultDB = QSqlDatabase::addDatabase( DBDRIVER );
defaultDB->setHostName( DB_HOSTNAME );
defaultDB->setDatabaseName( "DRIVER={MySQL};Server="+DB_HOSTNAME+";Database="+DB_NOM+";Uid= "+DB_USR+";Pwd="+DB_PASS+";" );
defaultDB->setUserName( DB_USR );
defaultDB->setPassword( DB_PASS );

if ( ! defaultDB->open() ) {
QMessageBox::warning(0, "Fallo de conexión", "No se pudo conectar con la base de datos: " + DB_NOM + "tabla: " + DB_TABLA + "\n" + defaultDB->lastError().driverText());
return FALSE;
}
QMessageBox::information(0,"Conexión estabecida","Conectado a: " + DB_HOSTNAME + " " + "tabla: " + DB_TABLA + " "+ DB_NOM);
return TRUE;
}

wysota
7th June 2006, 18:39
Check if defaultDB!=0 before dereferencing the pointer (line 6).

Philip_Anselmo
7th June 2006, 19:01
QSqlDatabase *defaultDB;
bool creaConexion()
{

defaultDB = QSqlDatabase::addDatabase( DBDRIVER );
if (defaultDB!=0)
{
defaultDB->setHostName( DB_HOSTNAME );
qWarning("host seteado");
defaultDB->setDatabaseName( "DRIVER={MySQL};Server="+DB_HOSTNAME+";Database="+DB_NOM+";Uid= "+DB_USR+";Pwd="+DB_PASS+";" );
defaultDB->setUserName( DB_USR );
defaultDB->setPassword( DB_PASS );

if ( ! defaultDB->open() )
{
QMessageBox::warning(0, "Fallo de conexión", "No se pudo conectar con la base de datos: " + DB_NOM + "tabla: " + DB_TABLA + "\n" + defaultDB->lastError().driverText());
return FALSE;
}
QMessageBox::information(0,"Conexión estabecida","Conectado a: " + DB_HOSTNAME + " " + "tabla: " + DB_TABLA + " "+ DB_NOM);
qWarning("No pesko");
return FALSE;
}
}


It stills says segment violation :(

If I comment the last part.. from if ( ! defaultDB->open() ) 'till the last return
It doesn't tells me the segment violation stuff.. but when I try to fill the qdatatable with a qsqlcursor the segment violation stuff comes out :crying:

so I have to find a way to do that :S any ideas?
because I don't have any :(

wysota
7th June 2006, 19:11
Try with some other driver (like QSQLITE or better yet with QMYSQL) to see if your code is alright. If it won't crash, then it means there is something wrong with the driver.

Philip_Anselmo
7th June 2006, 21:28
I've tryed with QMySQL3 and it works perfectly, but i'm not able to use the qmysql on windows so I can't use it now because I need to work with the same code on Linux Qt 3.1.2 and windows Qt 4.1.2-3

My question is.. once I made the connection with odbc, the workaround with qsqlcursor is the same as with qmysql3?

wysota
7th June 2006, 22:13
I've tryed with QMySQL3 and it works perfectly, but i'm not able to use the qmysql on windows
Why not?

so I can't use it now because I need to work with the same code on Linux Qt 3.1.2 and windows Qt 4.1.2-3
You won't be, because Qt3 and Qt4 database interfaces differ (for example addDatabase of one of them returns a pointer and in the other -- an object).


My question is.. once I made the connection with odbc, the workaround with qsqlcursor is the same as with qmysql3?

The general idea of using Qt database abstraction is that it is abstract -- it doesn't depend on the driver used. You should be able to use ODBC and MYSQL drivers using the same code. The only line which differs will be the one which chooses the driver type, but you can work it around using #ifdef-#endif constructions.

Philip_Anselmo
7th June 2006, 23:08
I know the diference on qt3 and 4 with the pointer on the database connection

the problem on using qmysql on windows... well at this time I just think it's imposible... at least everything I've tryed didn't work I just give up on that.. if you search all my posts here I think you'll find out that 4/5 of them are about the qmysql pluginI even made a how-to about it and it worked for others but not here on 3 windows!!! Qt hates me :(

I just hope ODBC works... if not.. well.. no more qt+mysql for me


by the way I think it isn't connecting:



QSqlDatabase *defaultDB;
bool creaConexion()
{
defaultDB = QSqlDatabase::addDatabase( DBDRIVER );
if (defaultDB!=0)
{
defaultDB->setHostName( DB_HOSTNAME );
qWarning("host seteado");
defaultDB->setDatabaseName( "Driver={MySQL};Server="+DB_HOSTNAME+";Database="+DB_NOM+";Uid="+DB_USR+";Pwd="+DB_PASS+";" );
defaultDB->setUserName( DB_USR );
defaultDB->setPassword( DB_PASS );

if ( ! defaultDB->open() )
{
QMessageBox::warning(0, "Fallo de conexión", "No se pudo conectar con la base de datos: " + DB_NOM + "tabla: " + DB_TABLA + "\n" + defaultDB->lastError().driverText());
return FALSE;
}
QMessageBox::information(0,"Conexión estabecida","Conectado a: " + DB_HOSTNAME + " " + "tabla: " + DB_TABLA + " "+ DB_NOM);
qWarning("No pesko");
return TRUE;
}
}

the qWarning tells me:


QODBC3: Unable to connect


and on console:


QODBCDriver::disconnect: Unable to disconnect datasource Error: [unixODBC][Driver Manager]Connnection does not exist

wysota
8th June 2006, 01:14
the problem on using qmysql on windows... well at this time I just think it's imposible... at least everything I've tryed didn't work I just give up on that.. if you search all my posts here I think you'll find out that 4/5 of them are about the qmysql pluginI even made a how-to about it and it worked for others but not here on 3 windows!!! Qt hates me :(


You're probably making the same mistake all the time. But doesn't Qt for Windows come with a precompiled binary mysql driver? Or maybe you could ask someone else to compile the driver for you? There is really no reason to reinvent the wheel...

Philip_Anselmo
8th June 2006, 01:58
I-ve done that :(
but even when the plugin is present it tells me driver not loaded

wysota
8th June 2006, 11:07
Did you check its dependencies? Among other, it depends on mysqlclient library... It has to be available and has to be found by the plugin.

Philip_Anselmo
8th June 2006, 15:40
yeap I checked that too
and it also depends on MSJAVA.dll but it weren't available
but I've never even thought about using java on neither one of the 2 pcs where i've compiled and made my app, and according to JaCek and I belived him that, that's not the problem

but well about the ODBC? any ideas? anyone else read this? :p
I don't want to bother you too much wysota :o

can anyone post an example code of a connection to mysql through ODBC that fills a datatable or some structure like that or simplily uses a cursor? Please :(

I uploaded my proyect so you could dowload it from this address (http://rapidshare.de/files/22538234/pro1_QODBC.zip.html)

wysota
8th June 2006, 17:08
You're not bothering me, don't worry.

Which version of mysql do you use anyway?

BTW. I don't know if MySQL through ODBC supports cursors...

Philip_Anselmo
8th June 2006, 17:52
I'm using mysql Ver 11.18 Distrib 3.23.58, for redhat-linux-gnu (i386), Qt 3.1.2,Fedora Core 1,ODBC 2.2.8, MyODBC-2.50.39-17, g++ (GCC) 3.3.2 20031022

here (http://rapidshare.de/files/22544250/log.strace.html) you can see a log of my app's strace

wysota
8th June 2006, 20:25
I'm starting to be getting lost... Last time you were talking about Qt4 and Windows, now about Qt3 and Linux... Could you make up your mind? :) Which driver can't you enable?

Philip_Anselmo
8th June 2006, 22:25
jajaja

I'm programming on the fedora everything is made here on the fedora, the qmysql3 pluging works perfect here on Linux but not on windows with qt4 and I have to make it so it works on both Linux Qt 3.1.2 and windows Qt 4.1.2
on windows I can't make the plugin to work.. it compiles some times but it still tells me driver not loaded so the qmysql3 is out of the question because it works only on linux
now the odbc isn't working here on the linux so I haven't tested it yet on windows
the most important right now is that it has to work on windows, because the final users will, the most of the time (not to say always), work on windows. Personaly I prefer Linux to work and wintendo as a game console :p and the office, but that's me.
So I have to make it work on windows.

wysota
8th June 2006, 22:38
I'm programming on the fedora everything is made here on the fedora, the qmysql3 pluging works perfect here on Linux but not on windows with qt4 and I have to make it so it works on both Linux Qt 3.1.2 and windows Qt 4.1.2

So maybe it's better to switch to Qt4 on Linux too? Why develop two apps instead of one?


on windows I can't make the plugin to work.. it compiles some times but it still tells me driver not loaded
Maybe it can't find some required component? Like the mysql library I already mentioned.


so the qmysql3 is out of the question because it works only on linux
So what? Use qmysql on Linux and qodbc on Windows... Who said you have to use the same driver?

Philip_Anselmo
8th June 2006, 23:08
I can't install qt 4.1.2 here because of some libraries.. fedora core 1...... :rolleyes:

the libmysqlclient is present when i compile

and well the lastone.. i don't know maybe I just could do that but that depends on my boss's opinion.. :(
and the 1º one also depends on my boss xD
if I could I would use Debian Sarge...
I need a notebook :crying:
I just wish I had an Alienware

so any ideas about this?:


QODBCDriver::disconnect: Unable to disconnect datasource Error: [unixODBC][Driver Manager]Connnection does not exist


I just founded a post on qtforum that someone has the same problem.. but on that forum none ever answers:mad:

wysota
8th June 2006, 23:37
I can't install qt 4.1.2 here because of some libraries.. fedora core 1...... :rolleyes:
Compile it yourself from sources. It's as easy as ./configure (you'll need to pass some options here) && make && make install.


the libmysqlclient is present when i compile
It's important if it's present (meaning, if the driver can find it) when you execute your app.



so any ideas about this?
Don't worry about it. IMO you should use the native mysql driver (and use Qt4 instead of Qt3).


I just founded a post on qtforum that someone has the same problem.. but on that forum none ever answers:mad:

Most "answerers" from QtForum came here with us after the big fuzz during the fall last year. In my opinion QtForum is already dead because of that. I'm just wondering why people keep asking questions there and the only reason I can think of is that QtForum is much higher in Google rankings than QtCentre. Sorry for offtopic :)

Philip_Anselmo
9th June 2006, 16:15
jajaja ok you convinced me... I'm going to try install the last Qt, the 4.1.3 Open Source, on my home :p I just want to try it works fin and the qmysql plugins stuff too so i don't lose the Qt working here.I've lost a windows qt+qmysql working :crying: I'm not ready to lose another one Dr. :crying:
xD

are you shure there won't be any problems compiling Qt 4.1.3 on a fedora core 1? core 1! this ancient relic won't install libc++6 :o and well I'm installing everything by yum and rpm... because Im kind of new to linux too

wysota
9th June 2006, 17:03
I just want to try it works fin and the qmysql plugins stuff too so i don't lose the Qt working here.
You won't. Qt3 and Qt4 can be installed side by side (Qt4 doesn't make Qt3 obsolete! For example KDE won't work without Qt3).


are you shure there won't be any problems compiling Qt 4.1.3 on a fedora core 1?
Sure. Don't worry. You won't break anything. I see that my Qr4 links against libstdc++6, but it'll probably link with ver. 5 too.