c++ - 使用 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;
}
解决方案
推荐阅读
- javascript - JavaScript 数组过滤器未按预期工作
- python - 前兄弟在 XPath 和 Python 中是如何工作的?似乎显示错误的输出
- python - django Rest框架中的py manage.py dumpdata withoutrest.employee --indent 4
- javascript - 流类型:如何从类中创建精确类型的实例
- c# - 重置(和清理内存)StringBuilder 的最佳方法
- alpha-vantage - 从 AlphaVantage 获取实时股票价格
- c++ - C++ 范围与手动 for 循环
- node.js - Node.js Javascipt 对象覆盖而不是合并
- wpf - WPF 绑定可见函数
- java - 在没有 ApplicationContext 文件的情况下测试 Spring 控制器服务