QT源吗:
#include <qapplication.h>
#include <qsqldatabase.h>
int main( int argc, char *argv[] )
{
QApplication app( argc, argv );
QSqlDatabase *defaultDB = QSqlDatabase::addDatabase("QPSQL7");
if (!defaultDB )
{
qWarning("Concceted Failed!");
}
defaultDB->setDatabaseName( "test" );
defaultDB->setUserName("
ostgre");
defaultDB->setPassword("123123");
defaultDB->setHostName("localhost");
if ( !defaultDB->open() )
{
qWarning("Open DataBase Failed");
}
return 0;
}
编译:
qmake -project connect.cpp
qmake
make
g++ -c -pipe -Wall -W -I/usr/include/fontconfig -I/usr/include/Xft2 -O2 -march=i386
-mcpu=i686 -fno-use-cxa-atexit -fno-exceptions -DQT_NO_DEBUG -I/usr/lib/qt3-gcc3.2/include
-I/usr/lib/qt3-gcc3.2/mkspecs/default -o connect.o connect.cpp
connect.cpp:23:6: warning: no newline at end of file
g++ -o temp connect.o -L/usr/lib/qt3-gcc3.2/lib -L/usr/X11R6/lib -lqt-mt -lXext -lX11
-lm
执行:
./temp
QSqlDatabase warning: QPSQL7 driver not loaded
QSqlDatabase: available drivers:
Open DataBase Failed
可以肯定PostgreSQL数据库在后台运行无误,而且用ODBC可正确的建立连接,可将
QSqlDatabase *defaultDB = QSqlDatabase::addDatabase("QPSQL7");改为:QSqlDatabase *defaultDB
= QSqlDatabase::addDatabase("QODBC3");仍报类似错
QSqlDatabase warning: QODBC3 driver not loaded
QSqlDatabase: available drivers:
Open DataBase Failed
还有一个用gcc的例子帮我分析一下:
#include <stdio.h>
#include <libpq-fe.h>
int main()
{
PGconn *conn;
const char *connectStr="host=localhost dbname=test";
conn = PQconnectdb(connectStr);
if(PQstatus(conn)==CONNECTION_BAD)
{
printf("Connection to failed");
}
else printf("Connected Ok\n");
PQfinish(conn);
return 1;
}
编译:
g++ connect.c -I/usr/local/pgsql/include -L/usr/local/pgsql/lib -o connect -lpq
执行:
./connect
./connect: error while loading shared libraries: libpq.so.3: cannot open shared
object file: No such file or directory
可以肯定:/usr/local/pgsql/lib/libpq.so.3 肯定存在 且libpq.so,libpq.so.3.0也存在。