Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用pqxx从postgres读取/写入大型对象_C++_Iostream_Libpqxx_Lob_Largeobject - Fatal编程技术网

C++ 使用pqxx从postgres读取/写入大型对象

C++ 使用pqxx从postgres读取/写入大型对象,c++,iostream,libpqxx,lob,largeobject,C++,Iostream,Libpqxx,Lob,Largeobject,主pqxx API使用列作为文本。那么,如何使用pqxx库从大型对象(LOB)访问二进制数据呢?有几种方法。第一种方法是将数据转换为bytea或从bytea转换为bytea,并通过通用的pqxx api进行工作。如果你知道如何使用bytea,也许这就是你的方式。这里是一个例子,如何插入一个字符串作为LoB,普通SQL,没有C++代码:< /P> select lo_from_bytea(0, 'this is a test'::bytea); ... select encode(lo_get(1

主pqxx API使用列作为文本。那么,如何使用pqxx库从大型对象(LOB)访问二进制数据呢?

有几种方法。第一种方法是将数据转换为bytea或从bytea转换为bytea,并通过通用的pqxx api进行工作。如果你知道如何使用bytea,也许这就是你的方式。这里是一个例子,如何插入一个字符串作为LoB,普通SQL,没有C++代码:< /P>
select lo_from_bytea(0, 'this is a test'::bytea);
...
select encode(lo_get(190850), 'escape'); -- here 190850 is the oid for lob created by the first line.
另一个选项是使用pqxx库提供的iostream API。关于如何使用它,没有太多的例子,因此我们开始:

// write lob
auto conn = std::make_shared<pqxx::connection>(url);
auto tran = std::make_shared<pqxx::work>(*conn);
auto stream = std::make_shared<pqxx::olostream>(*tran, oid);
stream->write(data, size);
stream->flush();
stream.reset();
tran->commit();

// read lob
stream = std::make_shared<pqxx::ilostream>(*tran, oid);
...
sszie_t get_chunk(shard_ptr<> stream, char *buf, size_t max_len)
{
    while (!stream->eof() && len < max_len && stream->get(buf[len])) {
        len++;
    }

    return (len > 0 || !stream->eof()) ? len : -1;
}
//写lob
自动连接=标准::使共享(url);
自动传输=标准::使共享(*conn);
自动流=标准::使共享(*tran,oid);
流->写入(数据、大小);
流->刷新();
stream.reset();
tran->commit();
//读lob
stream=std::使_共享(*tran,oid);
...
sszie_t get_chunk(碎片ptr流,字符*buf,大小\u t max\u len)
{
而(!stream->eof()&&lenget(buf[len])){
len++;
}
返回(len>0 | |!stream->eof())?len:-1;
}
注意:pqxx::ilostream中有一个bug,如果数据中的0xff字节将命中内部缓冲区边界,则可能会得到截断的数据,它将被错误地视为EOF字符。该漏洞在2020年2月被修复,目前该修复并未覆盖所有发行版