简体   繁体   中英

How to access Postgresql using Qt in Windows

I am developing an application using QtRuby and PostgreSQL. I successfully developed and running successfully in Linux. Now i want to run the same application in windows. But I am failing to connect to database(postgresql). I am getting the following errors:

 QSqlDatabase: QPSQL driver not loaded
 QSqlDatabase: available drivers: QSQLITE QODBC3 QODBC

As the error message says, the QPSQL driver is not available for Qt to talk to Postgres with. Probably the plugin has not been compiled yet.

As this how-to indicates, you need to install the PostgreSQL developer libraries, and then manually build the Qt driver plugin. See the how-to for the exact commands.

Postgresql using QT in Windows:

1) check installation directory, in my pc:

PostgreSQL (32 bit)    C:\PSQL32\bin
Qt (32 bit)            C:\QT\4.8.0\bin
MingW32                C:\QT\qtcreator-2.4.0\mingw\bin      

2) add these 3 directory to system PATH

3) Download 'mingw-utils' and install in C:\\Qt\\4.8.0\\bin (need utility REIMP.exe)

4) Convert libpq.lib in useful format for mingw32:

CD C:\\PSQL32\\lib

REIMP -d libpq.lib

Open libpq.def and delete ALL "_" at the beginning of definitions

dlltool --input-def libpq.def --output-lib libpq.a --dllname libpq.dll

5) Compile:

CD C:\\Qt\\4.8.0\\src\\plugins\\sqldrivers\\psql

qmake -o Makefile "INCLUDEPATH+=C:\\PSQL32\\include" "LIBS+=C:\\PSQL32\\lib\\libpq.a" psql.pro

mingw32-make debug

mingw32-make release

10) copy dll:

libqsqlpsqld4.a   qsqlpsqld4.dll    (debug)  

libqsqlpsql4.a   qsqlpsql4.dll      (release)

in the directory:

C:\Qt\4.8.0\plugins\sqldrivers

there is a multiple definitions of struct timespec

in file of PostgreSQL (C:\\PSQL32\\include\\pthread.h) 文件中(C:\\ PSQL32 \\ include \\ pthread.h)

replace:

#ifndef HAVE_STRUCT_TIMESPEC

with:

#if ((!defined(_TIMESPEC_DEFINED)) && (!defined(HAVE_STRUCT_TIMESPEC)))


Done :-)

Great! It's done! However there are some tips to pay attention: 1. Mingw-utils, 0.4-1 will not work on Windows 7, we must use 0.3. 2. There is a psql.json file in the directory of PSQL driver, please don't delete it.

  1. Compile the QPSQL plugin as instructed here: http://ldc.usb.ve/docs/qt/sql-driver.html#qpsql-for-postgresql-version-7-3-and-above
  2. Add the PostgreSQL bin path to your PATH environment variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM