我创建的表格如下:
CREATE TABLE tbl_test
(
id bigserial PRIMARY KEY,
interest int ARRAY[2]
);
Run Code Online (Sandbox Code Playgroud)
我PGresult* res用了PQexec(conn, "SELECT * FROM tbl_test");
现在,我怎样才能int[]从PQgetvalue(res, 0, 1).
我不想依赖于定义的结构,array.h因为它们可能会改变.
我在Postgresql文档中找不到任何可以执行操作的API.
请指教.
此致,
Mayank
我正在使用libpq处理postgresql.下面给出的代码花费了很多时间(代码末尾给出的时间).
#include "stdafx.h"
#include <stdlib.h>
#include <libpq-fe.h>
#include <windows.h>
static void exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
int _tmain(int argc, _TCHAR* argv[])
{
const TCHAR *conninfo;
PGconn *conn;
PGresult *res;
int nFields, i, j;
if (argc > 1)
conninfo = argv[1];
else
conninfo = _T("hostaddr=192.168.4.171 port=12345 dbname=mydb user=myname password=mypass");
conn = PQconnectdb(conninfo);
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
exit_nicely(conn);
}
/* Start a transaction block */
res = PQexec(conn, "BEGIN");
if (PQresultStatus(res) != PGRES_COMMAND_OK) …Run Code Online (Sandbox Code Playgroud) 我是PostgreSQL / libpq的新手。因此,请帮助我澄清我的困惑:
假设我从执行“ START TRANSACTION”开始并进行了正确的错误检查(PQresultStatus(res)!= [proper_success_value]),如果插入后出现问题,是否需要执行“ ROLLBACK”?例如:
在这种情况下,我是否需要在“ UPDATE”失败后执行“ ROLLBACK”?另外,如果“ ROLLBACK”也失败怎么办?
我遇到了libpq PQexec函数挂在间歇性连接上的问题.浏览邮件列表后,解决方案是使用异步函数PQsendQuery/ PQgetResult并实现自己的超时.
现在我面临的问题是PQgetResult需要多次调用,直到它返回null,然后你就知道它已经完成了.但是,我的应用程序的其余部分需要PQresult每个查询一个对象.
所以我的问题是:
PQresults?PQisBusy&PQconsumeInput等到所有结果都准备好了吗PQgetResult?所以我有以下问题:
g++ -std=c++0x -O3 -I/usr/include/scip main.cpp locations/locationreader.cpp locations/locationassigner.cpp scheduler.o optimizer.o dbmanager.o scheduleData.o exam.o -o main -L/usr/lib -lscip -lreadline -lgmp -lz -llpispx -lsoplex -lobjscip -lnlpi.cppad -lscipopt -lzimpl -lpqxx -lpq -pthread
/usr/lib/../lib64/libpqxx.so: undefined reference to `PQescapeIdentifier'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么它找不到引用,因为我已经安装了libpq并且它位于/ usr/lib64中
$ ls | grep pq
libpq.so
libpq.so.5
libpq.so.5.2
libpqxx-4.0.so
libpqxx.so
Run Code Online (Sandbox Code Playgroud)
所以,如果我能得到任何帮助,那将是可爱的
为什么下面的代码使用1.2GB的内存来执行?无论传递给 generate_series 的数字如何,我都希望内存使用量相对平稳,相反它正在稳步增加。请 - 告诉我我做错了什么!
if (!PQsendQuery(conn, "select generate_series(1, 10000000)"))
exit(1);
int i, value;
while (res = PQgetResult(conn)) {
for (i = 0; i < PQntuples(res); i++) {
value = atoi(PQgetvalue(res, i, 0));
}
PQclear(res);
}
printf("%d\n", value);
PQfinish(conn);
Run Code Online (Sandbox Code Playgroud)
我已将本示例的完整源代码放在pastebin 上。
我正在使用libpq. 我收到此错误INSERT failed: ERROR: insufficient data left in message。
这是我的代码库中的相应片段:
printf ("Enter write parameter_value");
scanf("%f", ¶meter_value);
char *stm = "INSERT INTO write_reg_set (parameter_value) VALUES ($1::double precision)";
int nparam = 1;
//set the values to use
const char *values[1] = {(char *)¶meter_value};
//calculate the lengths of each of the values
int lengths[1] = {sizeof(parameter_value)};
//state which parameters are binary
int binary[1] = {1};
PGresult *res = PQexecParams(conn,
stm,
nparam, //number of parameters
NULL, //ignore the Oid field …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 libpq 编译 C++ 应用程序。在我的 Debian 上,使用以下命令可以正常编译:
g++ -I /usr/include/postgresql/ -L /usr/lib/postgresql/9.3/lib/ -lpq -o test test.cpp
Run Code Online (Sandbox Code Playgroud)
在我的 Ubuntu 上,我收到以下错误:
undefined reference to 'PQfinish'
Run Code Online (Sandbox Code Playgroud)
我已经包含了 postgresql 标头和库的链接并使用了 -lpq。我缺少什么?
我几天来一直在网上搜索,但我无法在我的 Linux 机器(CentOS - 2.6.32-431.3.1.el6.x86_64 GNU/Linux)上安装 psycopg2 库。
我知道问题是我缺少 libpq 头文件,因为我在尝试 pip install psycopg2 后收到此消息: libpq-fe.h: No such file or directory
http://initd.org/psycopg/docs/install.html#install-from-source
我发现的几乎所有文章都指出我在 CentOS 上使用 apt-get 但 apt-get 不是 CentOS 6.3 上的标准工具,所以我一直在尝试yum install。
但是,每次我尝试使用sudo yum install下载的东西时,软件包都不可用。例如:
yum install postgresql-devel
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
drivesrvr | 2.2 kB 00:00
No package postgresql-devel available.
Error: Nothing to do
我试过这个:
我正在尝试运行我的 python 脚本,但出现以下错误。我尝试以不同的方式安装和卸载 psycopg2,但没有奏效。我的 postgres 版本是 9.6,我的 psycopg2 版本是 2.8.2。有人遇到过同样的问题吗?提前致谢。
Traceback (most recent call last):
File "server.py", line 89, in <module>
cfg = Config(application, WSGI_PATH_PREFIX)
File "server.py", line 60, in __init__
self.mgrs = managers.register_managers(self.app)
File "api/managers_fourwalls.py", line 31, in register_managers
return Managers(app)
File "api/managers_fourwalls.py", line 41, in __init__
self.FourwallsEngine = four_walls({"posgres_db": self.DataManager.pg_connection_string})
File "api/engine/four_walls_engine.py", line 14, in __init__
self.engine = create_engine(self.db_string)
File "/usr/lib64/python2.7/site-packages/sqlalchemy/engine/__init__.py", line 387, in create_engine
return strategy.create(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 80, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File …Run Code Online (Sandbox Code Playgroud) libpq ×10
postgresql ×8
c ×5
c++ ×3
libpqxx ×2
asynchronous ×1
centos ×1
database ×1
fedora ×1
g++ ×1
psycopg2 ×1
python ×1
python-2.7 ×1
transactions ×1