使用libpq的远程机器PostgreSQL中插入二进制大对象(BLOB) [英] Insert Binary Large Object (BLOB) in PostgreSQL using libpq from remote machine

查看:537
本文介绍了使用libpq的远程机器PostgreSQL中插入二进制大对象(BLOB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能给使用的libpq从远程计算机中插入PostgreSQL数据库二进制数据的例子。
我的第二个问题是:是否有任何其他API比用C ++ LIBPQ更有效率。
谢谢

Can you give an example of inserting binary data in PostgreSQL database from remote machine using libpq. My second question is: Is there any other API more efficient than libpq with C++. Thanks

推荐答案

有2种类型的斑点的PostgreSQL中— BYTEA 大对象。我建议你​​不要使用大对象,你不能将它们加入到表中。

There are 2 types of blobs in PostgreSQL — BYTEA and Large Objects. I'd recommend against using large objects as you can not join them to tables.

有关BYTEA你使用libpq的是这样的:

For BYTEA you'd use something like this in 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;
}

这篇关于使用libpq的远程机器PostgreSQL中插入二进制大对象(BLOB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆