PDA

View Full Version : Access to PostgreSQL DB on a linux server



rmagro
30th January 2008, 08:41
Hi All,

My application is trying to connect to a PostgreSQL DB which has been created on a linux server (red hat 4)

I am using the 4.3.3 version of Qt that I installed on my Home path.

Follows the essential code I was trying to use to get theconnection to the db.
It is simply a main.cpp:


#include <QtCore>
#include <QCoreApplication>

#include <QtSql>

//#include <QSqlDatabase>
//#include <QSqlDriver>
//#include <QSqlQuery>

#define DB_DRIVER "QPSQL"
#define DB_HOST "127.0.0.1"
#define DB_DBNAME "myDBname"
#define DB_USER "myDBuser"
#define DB_PASSWD "myDBpwd"


int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);


QSqlDatabase myDB = QSqlDatabase::addDatabase( DB_DRIVER );

myDB.setHostName( DB_HOST );
myDB.setDatabaseName( DB_DBNAME );
myDB.setUserName( DB_USER );
myDB.setPassword( DB_PASSWD );

myDB.setConnectOptions("connect_timeout=15");

bool okDB = myDB.open();

if (!okDB) {
printf("Error while trying to open the DB \n");
//qWarning( "Error while trying to open the DB: "+ myDB.lastError().text() );
return FALSE;
}
}


When I run the application..I always get the following:

QSqlDatabase: QPSQL driver not loaded
QSqlDatabase: available drivers:
Error while trying to open the DB

What should I do ?
Can someone help me?

Thank you very much..

Roberto

jpn
30th January 2008, 08:53
Hi, try this: How to Build the QPSQL Plugin on Unix and Mac OS X (http://doc.trolltech.com/4.3/sql-driver.html#how-to-build-the-qpsql-plugin-on-unix-and-mac-os-x)

rmagro
30th January 2008, 09:18
Thank you,

I tried what you suggested but I get the same answer when I run the application..

Is there another step to do?

Could it be somthing like it is not finding the lib??

Thank you

jpn
30th January 2008, 09:21
Just to assure, you did the "make install" step too, right? What's the contents of /path/to/qt/plugins/sqldrivers?

rmagro
30th January 2008, 09:33
No, I did not "make install", because on that machine I had not root privileges..
anyway I did ./configure -prefix-install..

Here is the list of files in that folder:

libqsqlite.so
libqsqlite.so.debug
libqsqlmysql.so
libqsqlmysql.so.debug
libqsqlpsql.so
libqsqlpsql.so.debug


Thanks

jpn
30th January 2008, 09:39
Hmm, what does

qmake -query QT_INSTALL_PLUGINS
output? Those plugins are in <output>/sqldrivers right? And what does

ldd libqsqlpsql.so
output?

rmagro
30th January 2008, 09:50
IN: qmake -query QT_INSTALL_PLUGINS
OUT:
/usr/local/Trolltech/Qt-4.3.3/plugins

mmhh strange ..that's not correct!!! it is a wrong path..



IN: ldd libqsqlpsql.so
OUT:
libpq.so.4 => /usr/lib/libpq.so.4 (0x004f9000)
libQtSql.so.4 => /home/fmas/qt-x11-opensource-src-4.3.3/lib/libQtSql.so.4 (0x00ad9000)
libQtCore.so.4 => /home/fmas/qt-x11-opensource-src-4.3.3/lib/libQtCore.so.4 (0x00d1e000)
libz.so.1 => /usr/lib/libz.so.1 (0x002c5000)
librt.so.1 => /lib/tls/librt.so.1 (0x00381000)
libpthread.so.0 => /lib/tls/libpthread.so.0 (0x00323000)
libdl.so.2 => /lib/libdl.so.2 (0x00f7c000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x008d2000)
libm.so.6 => /lib/tls/libm.so.6 (0x00c09000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x003cf000)
libc.so.6 => /lib/tls/libc.so.6 (0x00111000)
libssl.so.4 => /lib/libssl.so.4 (0x0023c000)
libcrypto.so.4 => /lib/libcrypto.so.4 (0x003d7000)
libkrb5.so.3 => /usr/lib/libkrb5.so.3 (0x00515000)
libcrypt.so.1 => /lib/libcrypt.so.1 (0x002d5000)
libresolv.so.2 => /lib/libresolv.so.2 (0x00729000)
libnsl.so.1 => /lib/libnsl.so.1 (0x00813000)
/lib/ld-linux.so.2 (0x005f9000)
libgssapi_krb5.so.2 => /usr/lib/libgssapi_krb5.so.2 (0x00777000)
libcom_err.so.2 => /lib/libcom_err.so.2 (0x00270000)
libk5crypto.so.3 => /usr/lib/libk5crypto.so.3 (0x00b6d000)

How do you justify this??

Thank you for your reply

rmagro
30th January 2008, 10:38
The Path /usr/local/Trolltech/Qt-4.3.3/plugins

does not exist..

How do make sure It will be find the righ path?

rmagro
30th January 2008, 11:13
Is there a way to set the QT_INSTALL_PLUGINS

jpn
30th January 2008, 12:01
This is all hardcoded to qmake by the time of configure. I guess something went wrong with configure.. At least the prefix seems to be not what you intended to.

rmagro
30th January 2008, 12:10
Ok. I will try other solutions.

Thanks a lot for you help anyway

jpn
30th January 2008, 12:11
Oh, I almost forgot. You can always place the plugin into sqldrivers-subdir of the application binary.


/path/to/app/binary
/path/to/app/sqldrivers/libqsqlpsql.so

rmagro
30th January 2008, 12:36
BINGO !!!

It worked fine!!

Thanks a lot ...you're great!

gQt
7th March 2008, 14:45
Hi,

I tried to run Robertos example but I can't find the libqsqlpsql files. I tried apt-cache search libsqlpsql with no result. My Qt-version is qt-4.3.3. In the path
/usr/local/Trolltech/Qt-4.3.3/plugins/sqldrivers the only files is:
libqsqlite.so libqsqlite.so.debug

I also tried to search psql in synaptic and downloaded:
postgresql-client
postgresql-client-7.4
postgresql-client-8.1
libqt3-mt-psql

I've got the libqsqlpsql.so but for qt3 so I still get the following errors when I'm running the program: (I tried to copy the libqsqlpsql.so for qt3 in the plugins/sqdrivers path for Qt4.33)

QSqlDatabase: QPSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE
Error while trying to open the DB

How can I find the libqslpsql files for qt4.3.3 ?
Any help is deaply apreciated!

gQt

jpn
7th March 2008, 22:25
How can I find the libqslpsql files for qt4.3.3 ?
If Ubuntu repositories don't offer such package you might have to download Qt sources and compile it yourself.

gQt
11th March 2008, 10:44
Thanks!

It looks like I have to configure and make Qt again. I can't find the Qt source code for Unix/linux available for downloads. When I type ./configure -help after downloading an unpacking the tar.gz-file I get these options:

{ two posts merged and ./configure -help output removed }

I guess I have to enable sql-pluggins and the psql-dev must be installed before ./configure, make. But I can't find any source/src option here? (My linux distribution is Debian) Any help is greatly apreciated!
Regards
gQt

jpn
11th March 2008, 10:57
It looks like I have to configure and make Qt again. I can't find the Qt source code for Unix/linux available for downloads.
ftp://ftp.trolltech.com/qt/source


When I type ./configure -help after downloading an unpacking the tar.gz-file I get these options:
Yes, anyone can run ./configure -help him/herself and get exactly the same output on X11.


I guess I have to enable sql-pluggins and the psql-dev must be installed before ./configure, make. But I can't find any source/src option here?
Yes, if you need SQL-plugins you either turn them on while building Qt by passing appropriate -sql-xxxxx options to configure OR you build the plugin manually (http://doc.trolltech.com/4.3/sql-driver.html) after Qt has been built.

gQt
11th March 2008, 13:38
Thanks for the links!

If I understand I can go to the QtDir and type: make confclean, then run the ./configure script with the wright options for sql pluggins: -pluggin-sql-<driver>

or build the QPSQL Plugin on Unix and Mac OS X manually
(for psql)

cd $QTDIR/src/plugins/sqldrivers/psql
qmake -o Makefile "INCLUDEPATH+=/usr/include/pgsql" "LIBS+=-L/usr/lib -lpq" psql.pro
make

My Qt-installation lack the src-dir under QTDIR and I can't find out how to download just the source. the link: ftp://ftp.trolltech.com/qt/source refers only to full Qt-installations, or am I wrong?

Is it a configure option to specify that the src-dir should be downloaded too ? I can't see that from the ./configure -options

jpn
11th March 2008, 14:35
Well, you can't re-compile Qt if you installed a binary package from your distribution's repository (because you don't have the sources). So the option is to go grab a source package, for example qt-x11-opensource-src-4.3.4.tar.gz (ftp://ftp.trolltech.com/qt/source/qt-x11-opensource-src-4.3.4.tar.gz), and compile that by yourself.

gQt
11th March 2008, 15:06
Thanks for replying!

That's the strange thing, I downloaded the qt-x11-commercial-src-4.3.3.tar.gz and this file should also be a source package, but I could remove the installation and install the opensource version instead.

jpn
11th March 2008, 19:42
That's the strange thing, I downloaded the qt-x11-commercial-src-4.3.3.tar.gz and this file should also be a source package, but I could remove the installation and install the opensource version instead.
Sorry, is this ... a question? :)

gQt
12th March 2008, 07:34
Hi,

The src-dir wasn't installed for some reason so I will copy the src-dir from the tar.gz-file to QtDir.

jpn
12th March 2008, 09:16
What? Wait! "make install" doesn't install sources. You are supposed to reconfigure Qt and/or rebuild Qt or its plugins in the original source directory. That's where you compiled Qt in the first place.

gQt
12th March 2008, 09:49
Hi,
Thanks!

I had to copy the srcdir from the .tar.gz file, for som reason the srcdir was not installed. qmake detect all the files:
qmake -o Makefile "INCLUDEPATH+=/usr/include/pgsql" "LIBS+=-L/usr/lib -lpq" psql.pro

But make gives a lot of errors and can't find libpq-fe.h. (This file is no where, tried locate). The errors in qsql_psql.cpp is probably because libpq-fe.h is missing.

the postgresql-client-8.1, postgresql-dev and postgresql-server-dev-8.1 is installed

I tried also to reconfigure qt. (make confclean first) and then
./configure -qt-sql-psql -plugin-sql-psql

gQt
12th March 2008, 12:05
Hi,

Finally make worked: qmake -o Makefile "INCLUDEPATH+=/usr/include/postgresql" "LIBS+=-L/usr/lib -lpq" psql.pro

The INCLUDEPATH had to be set to /usr/include/postgresql not /usr/include/pgsql

I still get the same errors when trying to run the database example. I have placed the libqsqlpsql.so in the folder you suggsted erlier, but I have no libqsqlpsql.so.debug file.

I guess I need this file, libqsqlpsql.so.debug, also?

jpn
13th March 2008, 07:36
Finally make worked: qmake -o Makefile "INCLUDEPATH+=/usr/include/postgresql" "LIBS+=-L/usr/lib -lpq" psql.pro

The INCLUDEPATH had to be set to /usr/include/postgresql not /usr/include/pgsql
Naturally if that's where your distribution installs them. :)


I still get the same errors when trying to run the database example. I have placed the libqsqlpsql.so in the folder you suggsted erlier, but I have no libqsqlpsql.so.debug file.
Just to assure, what does "ldd libqsqlpsql.so" output?

gQt
13th March 2008, 08:38
Thanks again jpn!

Maybe I have placed the file in the wrong dir?
I' m a little confused by this, I don't have a sqldrivers-subdir under bin if that what you mean?


You can always place the plugin into sqldrivers-subdir of the application binary.


/path/to/app/binary
/path/to/app/sqldrivers/libqsqlpsql.so




debian:/usr/local/Trolltech/Qt-4.3.3/bin# ls
assistant linguist.debug moc qt3to4 qtdemo.debug uic3.debug
assistant.debug lrelease moc.debug qt3to4.debug rcc uic.debug
designer lrelease.debug pixeltool qtconfig rcc.debug
designer.debug lupdate pixeltool.debug qtconfig.debug uic
linguist lupdate.debug qmake qtdemo uic3

libqsqlpsql.so is placed in this dir:


debian:/usr/local/Trolltech/Qt-4.3.3/sql/drivers# ldd libqsqlpsql.so
linux-gate.so.1 => (0xffffe000)
libpq.so.4 => /usr/lib/libpq.so.4 (0xb7eda000)
libQtSql.so.4 => /usr/local/Trolltech/Qt-4.3.3/lib/libQtSql.so.4 (0xb7ea2000)
libQtCore.so.4 => /usr/local/Trolltech/Qt-4.3.3/lib/libQtCore.so.4 (0xb7cfe000)
libz.so.1 => /usr/lib/libz.so.1 (0xb7cea000)
libgthread-2.0.so.0 => /usr/lib/libgthread-2.0.so.0 (0xb7ce6000)
libglib-2.0.so.0 => /usr/lib/libglib-2.0.so.0 (0xb7c54000)
librt.so.1 => /lib/tls/i686/cmov/librt.so.1 (0xb7c4a000)
libdl.so.2 => /lib/tls/i686/cmov/libdl.so.2 (0xb7c46000)
libpthread.so.0 => /lib/tls/i686/cmov/libpthread.so.0 (0xb7c34000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb7b4f000)
libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0xb7b2a000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7b1f000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb79ed000)
libssl.so.0.9.8 => /usr/lib/i686/cmov/libssl.so.0.9.8 (0xb79ae000)
libcrypto.so.0.9.8 => /usr/lib/i686/cmov/libcrypto.so.0.9.8 (0xb7874000)
libkrb5.so.3 => /usr/lib/libkrb5.so.3 (0xb77f8000)
libcom_err.so.2 => /lib/libcom_err.so.2 (0xb77f5000)
libcrypt.so.1 => /lib/tls/i686/cmov/libcrypt.so.1 (0xb77c7000)
/lib/ld-linux.so.2 (0x80000000)
libk5crypto.so.3 => /usr/lib/libk5crypto.so.3 (0xb77a1000)
libresolv.so.2 => /lib/tls/i686/cmov/libresolv.so.2 (0xb778e000)
libkrb5support.so.0 => /usr/lib/libkrb5support.so.0 (0xb7789000)

jpn
13th March 2008, 08:51
Yes, that's incorrect path. You shouldn't be creating any directories under /usr/local/Trolltech/Qt-4.x.y on your own. The proper directory is already there. You should place the plugin in

/usr/local/Trolltech/Qt-4.3.3/plugins/sqldrivers/libqsqlpsql.so

I believe invoking "make install" where you built the plugin should've been enough.

gQt
13th March 2008, 09:02
That helped a lot :)
Thanks !!