【问题标题】:protobuf C++ SQLite handle blob dataprotobuf C++ SQLite 处理 blob 数据
【发布时间】:2020-06-08 06:53:33
【问题描述】:

我有一个 SQLite 数据库,它有一个表,其中包含一些 BLOB 类型的字段。 我要做的是从数据库中获取字段(实际上也是所有其他字段)到 C++ 中,通过 protobuf 发送并接收 protobuf。

我在 .proto 文件中将 blob 字段定义为 bytes 例如

message fields{
    ...
    bytes myBlobField = 1;
}

我的 c++ 文件包含

sqlite3_initialize();
rc = sqlite3_open_v2(db_url, &db,SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,NULL);
std::ostringstream oss;
oss << "select * from attribtable ";
std::string query = oss.str();

rc = sqlite3_prepare_v2(db,query.c_str(),-1,&stmt,NULL

while(sqlite3_step(stmt) == SQLITE_ROW){

    sqlite3_column_blob(stmt,10) //This is the blob field
}

如何在 C++ 中存储 sqlite3_column_blob(stmt,10) 以及如何设置 myBlobField 使用 说reply-&gt;set_myblobfield(??)

并使用在客户端接收

say receive-&gt;get_myblobfield()

简而言之,我的问题是如何在 C++ 应用程序中将通过 protobuf 从数据库中获取的 blobfield 从服务器发送到客户端?

【问题讨论】:

  • fields 预处理成什么?
  • @RichardCritten 你能更清楚地说明你想问的问题吗?

标签: c++ blob protocol-buffers grpc proto


【解决方案1】:

使用这个 .proto 文件

syntax = "proto2";

package prototest;

message fields{
    required bytes myBlobField = 1;
}

您使用 set_myblobfield() 调用使用从 SQLite 获得的 blob 指针和 blob 的字节大小来初始化 blob,然后调用 SerializeToOstream() 方法将其写入流或文件。

std::ofstream myoutput("myoutput.bin");

while (sqlite3_step(stmt) == SQLITE_ROW)
{
    if (size_t blobSize = sqlite3_column_bytes(stmt, 10))
    {
        if (const void* blob = sqlite3_column_blob(stmt, 10))
        {
            prototest::fields myfields;
            myfields.set_myblobfield(blob, blobSize);

            myfields.SerializeToOstream(&myoutput);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2021-01-28
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    相关资源
    最近更新 更多