representing BLOBs in C++

2019-04-10 15:08发布

Are there any STL containers that seem to be well-suited for using as BLOBs for database software? I would think a vector<char>, but is there something better? Maybe a std::string? Or some non-STL container?

标签: c++ stl blob
3条回答
冷血范
2楼-- · 2019-04-10 15:49

The BLOB type of databases allows storage of binary data, so you need an ordered collection of bytes. The easiest choice would be a vector<> and you could chose unsigned char to represent a byte on most platforms

查看更多
Bombasti
3楼-- · 2019-04-10 15:51

We have used streams in one of our projects to represent BLOB/CLOB values stored in the database. I think this is most of the time the best approach, as BLOB/CLOBs could be really large to fit in memory by definition.

Write a stream implementation of your own and use it just like any other stream.

查看更多
Bombasti
4楼-- · 2019-04-10 15:54

I'm currently using std::string to store blobs, since I'm using Google's Protocol Buffers library for object serialization, and that's what they use (e.g., MessageLite::SerializeToString). It works well for my purposes since inserting the resulting string as a blob into an SQLite database is very straightforward:

sqlite3_bind_blob(_insert_statement, 3, data.c_str(), data.size(), SQLITE_STATIC);

(data is a std::string being bound as the third argument to _insert_statement.)

查看更多
登录 后发表回答