在Windows中使用Qt的Postgresql libpq [英] Postgresql libpq with Qt in windows

查看:279
本文介绍了在Windows中使用Qt的Postgresql libpq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用libpq.lib从qt连接到远程Postgresql数据库。我的机器上安装了postgresql,并且已经安装了ODBC驱动程序。我将数据库与java / jdbc一起使用。在我的Qt工作中,我似乎能做的就是让我的应用程序意外崩溃,而且似乎没有任何进展。



我的连接代码是

  PGconn *连接; 
char conninfo [250];

sprintf(conninfo, user =%s password =%s dbname =%s hostaddr =%s port =%d, user, password, foo, 192.168。 3.3英寸,5433);
qDebug()<< Foo1:<< conninfo;

连接= PQconnectdb(conninfo);
qDebug()<< Foo1:;

任何指导将不胜感激。

解决方案

直接使用 libpq 通过Windows ODBC API使用psqlODBC。您不需要两者。您似乎根本没有使用psqlODBC,因此我们可以忽略它,安装它并不重要。



您的代码看起来还可以,尽管您 really 应该使用 snprintf 来防止超出静态分配的conninfo缓冲区的大小,并通过缓冲区溢出将堆栈覆盖漏洞引入代码中。

还应该检查连接错误:

  if(PQstatus(connection )!= CONNECTION_OK)
{
qDebug()<< 连接失败:<< PQerrorMessage(连接);
出口(1);
}

我怀疑您遇到了与多个(可能不兼容)版本有关的问题 libpq 。您可能正在针对一个 libpq libpq.lib 进行编译,但是随后在运行时链接到另一个 libpq 。 Qt尤其如此,它提供了自己的SQL接口类,这些类可以链接到 libpq



发生的情况可能需要使用调试器。由于您使用的是Windows,因此表示Visual Studio,因为 windbg.exe 仅适用于受虐狂。如果您没有Visual Studio,请获取免费的Express版本这里。您需要用于Windows桌面的Express 2013​​。 (Microsoft在每个版本中以新的和令人困惑的方式更改名称)。在Visual Studio的调试器中运行程序,当程序崩溃时,请检查其原因。






@kiln指出存在 QSql ...,但是它非常有限,而且IMO不适合将数据库访问作为首要任务的生产应用程序。 (编辑:看起来它在Qt 5.4中要好很多;自Qt4起我就没有使用它了,他们终于修复了导致QtSQL在现实世界中几乎无法使用的最大遗漏,例如无法访问数据库中的本机SQLSTATE,缺少参数绑定等)。


I am trying to connect to a remote postgresql database from qt using libpq.lib. I have a postgresql installation on my machine and I have installed the ODBC drivers. I use the database with java/jdbc. In my Qt work All I can seem to do is get my application to crash unexpectedly and I can't seem to make any progress.

My connection code is

PGconn * connection;
char       conninfo[250];

sprintf(conninfo, "user=%s password=%s dbname=%s hostaddr=%s port=%d", "user", "password", "foo", "192.168.3.3", 5433);
qDebug() << "Foo1: " << conninfo;

connection = PQconnectdb( conninfo );
qDebug() << "Foo1: ";

Any guidance will be appreciated.

解决方案

Use libpq directly or use psqlODBC via the Windows ODBC APIs. You do not need both. You don't seem to be using psqlODBC at all, so we can ignore that, it doesn't matter that it's installed.

Your code looks OK, though you really should be using snprintf to prevent overrunning the size of your statically allocated conninfo buffer and introducing a stack overwrite exploit via buffer overflow into your code.

You should also check for connection errors:

if (PQstatus(connection) != CONNECTION_OK)
{
  qDebug() << "Connection failed: " << PQerrorMessage(connection);
  exit(1);
}

I suspect that you are having issues related to multiple, probably incompatible, versions of libpq. You are probably compiling against the libpq.lib for one libpq, but then at runtime linking to another libpq. This is especially likely with Qt, which provides its own SQL interface classes that may link to libpq.

To diagnose what's happening will probably require use of a debugger. Since you're on Windows, that means Visual Studio, because windbg.exe is only for masochists. If you do not have Visual Studio, get the free Express version here. You need "Express 2013 for Windows Desktop". (Microsoft changes the names in new and confusing ways with each release). Run your program under Visual Studio's debugger, and when it crashes, examine how/why.


As @kiln points out there's also QSql ... but it's very limited and IMO unsuitable for production applications where database access is a significant priority. (Edit: It looks like it's a lot better in Qt 5.4; I hadn't used it much since Qt4, and they've finally fixed the biggest omissions that made QtSQL practically unusable in the real world now, like the lack of access to the native SQLSTATE from the DB, lack of parameter binding, etc).

这篇关于在Windows中使用Qt的Postgresql libpq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆