在PostgreSQL中使用libpq从远程计算机插入二进制大对象(BLOB)
Insert Binary Large Object (BLOB) in PostgreSQL using libpq from remote machine
你能举一个使用libpq从远程机器在PostgreSQL数据库中插入二进制数据的例子吗?我的第二个问题是:有没有其他 API 比 libpq 和 C++ 更有效。谢谢
PostgreSQL 中有 2 种类型的 blob — BYTEA
和 Large Objects
。我建议不要使用大型对象,因为您无法将它们连接到表中。
对于 BYTEA,你可以在 libpq 中使用这样的东西:
PGresult* put_data_to_tablename(
PGconn* conn,
int32_t id,
int data_size,
const char* const data
) {
PGresult* result;
const uint32_t id_big_endian = htonl((uint32_t)id);
const char* const paramValues[] = { &id_big_endian, data };
const int nParams = sizeof(paramValues) / sizeof(paramValues[0]);
const int paramLenghts[] = { sizeof(id_big_endian), data_size };
const int paramFormats[] = { 1, 1 }; /* binary */
const int resultFormat = 0; /* text */
result = PQexecParams(
conn,
"insert into tablename (id, data) values ($1::integer, $2::bytea)",
nParams,
NULL, /* Types of parameters, unused as casts will define types */
paramValues,
paramLenghts,
paramFormats,
resultFormat
);
return result;
}
使用 libpqxx 是C++方法,而 libpq 是 C API。
以下是如何使用 pqxx 执行此操作的完整示例:如何使用 libpqxx API 将二进制数据插入 PostgreSQL BYTEA 列C++?
简而言之,使用 libpqxx 的相关C++行如下所示:
void * bin_data = ...; // obviously do what you need to get the binary data...
size_t bin_size = ...; // ...and the size of the binary data
pqxx::binarystring bin( bin_data, bin_size );
pqxx::result r = work.prepared( "test" )( bin ).exec();
work.commit();
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 预处理器:插入结构名称中的前一个行号
- 在未初始化映射的情况下,将值插入到映射的映射中
- 如何在c++中只将键插入到bimap的一侧
- 如何将结构插入到集合中并打印集合的成员
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- C++json插入数组
- Visual Studio 2019:插入多个C++风格的单行注释
- nlohmann-json将一个数组插入到另一个数组中
- 有效地使用std::unordered_map来插入或增加键的值
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 正在插入动态数组
- 插入或删除时获取usb的dos_name
- 叮叮当当在修复时插入多个"覆盖"说明符
- 链表c++插入,所有情况都已检查,但没有任何工作
- 将重物插入std::map
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 在PostgreSQL中使用libpq从远程计算机插入二进制大对象(BLOB)