首页 > 解决方案 > 使用 libpq 插入没有 timeZone 的二进制时间戳

问题描述

如何使用 libpq 和 PQexecParams 在表 car1 中插入二进制时间戳?代码有效,但表中的时间戳不正确

res = PQexec(conn, "CREATE TABLE IF NOT EXISTS car1 (id int, price float8, sale float8, delay timestamp);");

const char command[] = "INSERT INTO car1 (id, price, sale, delay)"
                         "VALUES($1::integer, $2::float8, $3::float8, $4::timestamp)";

  int nParams = 4;


  const Oid paramTypes[] = {23, 701, 701, 1114};
  const int paramLengths[] = {sizeof(int), sizeof(double), sizeof(double), sizeof(long long int)};
  const int paramFormats[] = {1, 1, 1, 1};
  int resultFormat = 0;

  int test = htonl(45);
  double test2;
  double test3;
  long long int test4 = htonl(1594817246);
  to_nbo(75.585, &test2);
  to_nbo(8.4785, &test3);


 const char *const paramValues2[] = {(const char*)&test, (const char*)&test2, (const char*)&test3, (const char*)&test4};

  /* PQexecParams INSERT */
  res = PQexecParams(conn, command, nParams, paramTypes, paramValues2, paramLengths, paramFormats, resultFormat);
  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
      std::cout << "PQexecParams failed: " << PQresultErrorMessage(res)
                << std::endl;
  }

标签: c++postgresqllibpq

解决方案


推荐阅读