I am working on a scientific project which requires several program abilities. After looking around for available tools I decided to work with Boost library which provided me needed features that C++ standard library does not provide such as date/time management, etc.
My project is set of command line which process a bunch of data from a old, homemade, plain-text file-based database: import, conversion, analysis, reporting.
Now I reached the point where I do need persistence. So I included boost::serialization that I found really useful. I am able to store and restore 'medium' dataset (not-so-big but not-so-small), they are about (7000,48,15,10)-dataset.
I also use SQLite C API to store and manage command defaults, output settings and variables meta informations (units, scale, limits).
Something crossed my mind: serialize into blob field instead of separate files. There might be some drawback that I haven't seen yet (there always is) but I think it can be a good solution that will suits my needs.
I am able to text-serialize into a std::string so I can do it that way: there is no difficulties because it only uses normal characters. But I would like to binary-serialize into a blob.
How should I proceed in order to use standard stream when filling my INSERT query?
Hah. I've never used sqlite3 C API before. And I've never written an output
streambuf
implementation. But seeing how I will probably be using sqlite3 in a c++ codebase in the future, I thought I'd spent some time withcppreference http://en.cppreference.com/w/cpp/io/basic_streambuf
So it turns out you can open a blob field for incremental IO. However, though you can read/write the BLOB, you can't change the size (except via a separate UPDATE statement).
So, the steps for my demonstration became:
blob_buf
object that derives fromstd::basic_streambuf<>
and can be used withstd::ostream
to write to that blobostream
It works :)
The code in
main
:You'll recognize the steps outlined.
To test it, assume you create a new sqlite database:
Now, once you have run the program, you can query for it:
If you enable the test code (that puts more data than the blob_size allows) you'll see the blob getting truncated:
Full Program
PS. I've kept error handling... very crude. You'll want to introduce a helper function to check sqlite3 errorcodes and translate into exception(s) maybe. :)