0

How can I please Insert Binary Timestamp in the table car1 using libpq and PQexecParams? The code works but the timestamp in the table is not correct

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;
  }

Botje
  • 26,269
  • 3
  • 31
  • 41
Derrick
  • 21
  • 3
  • Hi, welcome to StackOverflow. Please, follow this [guide](https://stackoverflow.com/help/minimal-reproducible-example) to produce a minimal reproductible example. – totok Jul 16 '20 at 08:59
  • Hi, Thanks i have done – Derrick Jul 16 '20 at 09:05
  • As with your previous questions: please add expected and actual outcome. Is the outcome simply off by a few hours or is it absolute nonsense? – Botje Jul 16 '20 at 09:40
  • Here is the outcome: 45 | 75.585 | 8.4785 | 219057-12-20 07:24:26.786816 The date and the time by the timestamp column are not correct – Derrick Jul 16 '20 at 10:45
  • @Botje Have you seen the outcome? – Derrick Jul 16 '20 at 13:08

0 Answers0