representing BLOBs in C++

萝らか妹 提交于 2019-12-06 04:34:25

问题


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?


回答1:


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




回答2:


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.




回答3:


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.)



来源:https://stackoverflow.com/questions/10672587/representing-blobs-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!