将数值绑定到准备好的sql的正确方法
Correct way to bind numeric values to prepared sql
我没有使用过libpq fe,(postgresql-9.1.3)
PGresult * pg_res;
char * sql = "INSERT INTO tbGroups (Group_UID, Full_Name, User_UID) VALUES ($1, $2, $3);";
int group_uid = 11;
int user_uid = 1;
const char * name = "TEST1";
const char * values[3] = {(const char *) &group_uid, name, (const char *) &user_uid};
int lengths[3] = {sizeof(group_uid), strlen(name), sizeof(user_uid)};
int format[3] = {1, 1, 1};
pg_res = PQprepare(conn, "insert_tbgroups", sql, 3, NULL);
PQclear(pg_res);
pg_res = PQexecPrepared(conn, "insert_tbgroups", 3, values, lengths, format, 1);
PQclear(pg_res);
有效,但结果是:
rcpdv=#从tbGroups中选择group_uid、full_name、user_uid;group_uid|full_name|user_uid-----------+---------------+----------184549376 |测试1 | 16777216(1行)
在没有param_types的情况下,将数值绑定到准备好的sql的正确方法是什么?
PostgreSQL期望数据通过网络连接。因此,您必须将数字数据转换为网络格式(big-endian)。我通常使用ntohl()
函数,在您的情况下举一个粗略的例子:
int format[3] = {1, 1, 1};
for (i = 0; i < 3; ++i) {
format[i] = ntohl(format[i]);
}
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 将数值绑定到准备好的sql的正确方法
- c++ /SIFT/SQL -如果有一种方法可以有效地比较图像的SIFT描述符与SQL数据库中的SIFT描述符