首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用libpq准备多行

使用libpq准备多行
EN

Stack Overflow用户
提问于 2020-07-08 16:13:32
回答 1查看 145关注 0票数 0

如何使用PQprepare通过libpq在一行中插入大约100行?

我不知道,也许我的参数是错误的..。

感谢您的回复。

代码语言:javascript
复制
const char command[] = "INSERT INTO car (id, name, price, day, time)"
                         "VALUES($1, $2, $3, $4, $5),"
                         "VALUES($1, $2, $3, $4, $5),"
                         "VALUES($1, $2, $3, $4, $5),"
                         "VALUES($1, $2, $3, $4, $5),"
                         "VALUES($1, $2, $3, $4, $5);";

  int nParams = 5;
  char* paramValues[5];
  int* paramLengths = new int[5];
  int* paramFormats = new int[5];

  res = PQprepare(conn, "insertStmt", command, nParams, NULL);
  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
    std::cout << "PQprepare failed:" << PQresultErrorMessage(res) << std::endl;
    PQclear(res);
  } else {
    PQclear(res);
    res = PQexecPrepared(conn, "insertStmt", nParams, paramValues, paramLengths,
                         paramFormats, resultFormat);
    if (PQresultStatus(res) != PGRES_COMMAND_OK) {
      std::cout << "PQexecPrepared failed: " << PQresultErrorMessage(res)
                << std::endl;
    }
    PQclear(res);
EN

回答 1

Stack Overflow用户

发布于 2020-07-08 19:43:15

代码语言:javascript
复制
const char command[] = "INSERT INTO car (id, name, price, day, time)"
                         "VALUES($1, $2, $3, $4, $5),"
                         "($6, $7, $8, $9, $10)";

  int nParams = 10;
  const char *const paramValues[] = {"2","P", "56", "1999-01-08", "1999-01-08 04:05:06",
                                     "3","P", "56", "1999-01-08", "1999-01-08 04:05:06"};

  const int paramLengths[] = {sizeof(int), sizeof(char), sizeof(double), sizeof(int),
                              sizeof(double),sizeof(int), sizeof(char), sizeof(double), sizeof(int), sizeof(double)};
  const int paramFormats[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  int resultFormat = 1;

  /* PREPARE INSERT */
  res = PQprepare(conn, "insertStmt", command, nParams, NULL);
  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
    std::cout << "PQprepare failed:" << PQresultErrorMessage(res) << std::endl;
    PQclear(res);
  } else {
    PQclear(res);
    res = PQexecPrepared(conn, "insertStmt", nParams, paramValues, paramLengths,
                         paramFormats, resultFormat);
    if (PQresultStatus(res) != PGRES_COMMAND_OK) {
      std::cout << "PQexecPrepared failed: " << PQresultErrorMessage(res)
                << std::endl;
    }
    PQclear(res);

  }

是工作!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62790357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档