gpt4 book ai didi

sql - libpq 如何传递批量数据

转载 作者:太空宇宙 更新时间:2023-11-03 23:41:40 27 4
gpt4 key购买 nike

我正在尝试将多行传递到测试表,但我无法理解它在 libpq 中究竟是如何完成的。

我找到了我认为需要的用于复制数据的命令,但没有关于如何使用它们的任何示例。 https://www.postgresql.org/docs/8.3/static/libpq-copy.html

这是我想出的代码,但我在 PQputCopyEnd 函数中遇到了段错误。我在这里很迷路,所以任何帮助都会很棒。

/*
* testlibpq.c
*
* Test the C version of libpq, the PostgreSQL frontend library.
*/
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <postgresql/libpq-fe.h>

static void exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}

int main(int argc, char **argv)
{
const char *conninfo, *errmsg;
PGconn *conn;
PGresult *res;

//std::string buffer = "key1\tcol11\tcol12\nley2\tcol21\tcol22";
std::string buffer = "key1\tcol11\tcol12";


if (argc > 1)
conninfo = argv[1];
else
conninfo = "dbname=postgres host=129.24.26.136 user=postgres password=postgresUNM";

/* Make a connection to the database */
conn = PQconnectdb(conninfo);

/* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
exit_nicely(conn);
}

//do stuff here
res = PQexec(conn, "COPY cplusplustest from STDIN");
int a = PQputCopyData(conn, buffer.c_str(), buffer.length());

res = PQexec(conn, "COMMIT");

int b = PQputCopyEnd(conn, errmsg);

if (errmsg == NULL)
{
printf("worked.\n");
}

/* close the connection to the database and cleanup */
PQfinish(conn);

return 0;
}

最佳答案

  • postgres-8.3 很旧;请考虑更新的版本
  • b = PQputCopyEnd(conn, errmsg); 的 errmsg 参数必须设置为 NULL(它是 libpq 的输入,表示客户端已经中止复制) ( the manual 仍然相当模糊,我同意。
  • 我删除了 C++。
  • COMMIT 应该在 CopyEnd 之后

/*
* testlibpq.c
*
* Test the C version of libpq, the PostgreSQL frontend library.
*/
#include <stdio.h>
#include <string.h> // <<--
#include <stdlib.h>

// #include <postgresql/libpq-fe.h> // <<--
#include <libpq-fe.h>

static void exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
int main(int argc, char **argv)
{
const char *conninfo, *errmsg;
PGconn *conn;
PGresult *res;
int a,b; // <<--

char buffer[] = "key1\tcol11\tcol12";

if (argc > 1)
conninfo = argv[1];
else
conninfo = "dbname=test host=/tmp/ user=postgres";

/* Make a connection to the database */
conn = PQconnectdb(conninfo);

/* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s"
, PQerrorMessage(conn));
exit_nicely(conn);
}

//do stuff here
errmsg = NULL; // << HERE
res = PQexec(conn, "COPY cplusplustest(key1,col11,col12) from STDIN;");
a = PQputCopyData(conn, buffer, strlen(buffer) );
b = PQputCopyEnd(conn, errmsg);

printf("Res=%p a=%d,b=%d\n", res, a, b);

if (errmsg )
printf("Failed:%s\n", errmsg);
else
printf("worked.\n");

res = PQexec(conn, "COMMIT;"); // <<-- HERE

/* close the connection to the database and cleanup */
PQfinish(conn);

return 0;
}

关于sql - libpq 如何传递批量数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43922809/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com