如何在 Windows 中使用 Qt 访问 Postgresql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/649519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:09:47  来源:igfitidea点击:

How to access Postgresql using Qt in Windows

windowspostgresqlqtruby

提问by user2476916

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:

我正在使用 QtRuby 和 PostgreSQL 开发应用程序。我在Linux上成功开发并成功运行。现在我想在 Windows 中运行相同的应用程序。但是我无法连接到数据库(postgresql)。我收到以下错误:

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

回答by MattJ

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.

正如错误消息所说,QPSQL 驱动程序不可用于 Qt 与 Postgres 对话。可能插件还没有被编译。

As this how-toindicates, 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 开发人员库,然后手动构建 Qt 驱动程序插件。有关确切命令,请参阅操作方法。

回答by Marek Miettinen

  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
  1. 按照此处的说明编译 QPSQL 插件:http://ldc.usb.ve/docs/qt/sql-driver.html#qpsql-for-postgresql-version-7-3-and-above
  2. 将 PostgreSQL bin 路径添加到 PATH 环境变量中

回答by Fausto Tomei

Postgresql using QT in Windows: How to compile driver QPSQL - MinGw - Win7

在 Windows 中使用 QT 的 Postgresql:如何编译驱动程序 QPSQL - MinGw - Win7

1) check installation directory, in my pc:

1)检查安装目录,在我的电脑中:

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

2) add these 3 directory to system PATH

2)将这3个目录添加到系统PATH

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

3)下载'mingw-utils'并安装在C:\Qt\4.8.0\bin(需要实用程序REIMP.exe)

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

4) 将 libpq.lib 转换为对 mingw32 有用的格式:

CD C:\PSQL32\lib

REIMP -d libpq.lib

光盘 C:\PSQL32\lib

REIMP -d libpq.lib

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

打开 libpq.def 并删除定义开头的所有“_”

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

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

5) Compile:

5)编译:

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

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 调试

mingw32-make 发布

10) copy dll:

10)复制dll:

libqsqlpsqld4.a   qsqlpsqld4.dll    (debug)  

libqsqlpsql4.a   qsqlpsql4.dll      (release)

in the directory:

在目录中:

C:\Qt.8.0\plugins\sqldrivers


NEW problem for QT version 5.01

QT 5.01 版的新问题

there is a multiple definitions of struct timespec

struct timespec 有多种定义

in file pthread.hof PostgreSQL (C:\PSQL32\include\pthread.h)

在PostgreSQL 的文件pthread.h(C:\PSQL32\include\pthread.h)

replace:

代替:

#ifndef HAVE_STRUCT_TIMESPEC

#ifndef HAVE_STRUCT_TIMESPEC

with:

和:

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

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



Done :-)

完毕 :-)

Faust

浮士德

回答by user2476916

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. Mingw-utils, 0.4-1 在Windows 7上是不行的,必须使用0.3。2、PSQL驱动目录下有psql.json文件,请勿删除。