Search code examples
c++databaseunqlite

How can I store a C++ container as a value in Unqlite?


The Unqlite NoSQL engine has a function unqlite_kv_store() which takes a key and a value to store. Is there a way to store a C++ container (e.g. list, vector, map, array) as a value and later retrieve it without using the document format?


Solution

  • Unless the container has its elements located contiguously, i.e. no links, no pointers, you can't store it in anything external to your program.

    The problem is your program or its data may be loaded at different addresses in memory. The addresses (pointers) you saved in the database are no longer valid.

    One method is to store your items in an array and then store the array as a BLOB type.

    But, a better solution for relational databases is to store you containers as separate tables; or appended to a single table. Look up "database for key". Also study "database normalization".