添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Usually SQLite does not offer a proper way to store arrays in the database. But if we consider how C++ pointers work, we can simply store arrays using SQLite Blob data type.

In SQLite documentation https://www.sqlite.org/datatype3.html it says “the value of blob data is stored as it is”.

Same documentation describes sqlite3_bind_blob https://www.sqlite.org/c3ref/bind_blob.html as

int sqlite3_bind_blob(sqlite3_stmt*, int param, const void* data, int n, void(*)(void*));

stmt – a pointer to sqlite3_stmt

param – index of the SQL parameter

data – value to bind to the parameter

n – no of bytes in the parameter

Let`s say we have a short array that need to be stored.

short array[] = {-256, -6128, 127};

Since sqlite3_bind_blob store the data as it is and it process the data in byte format, we can simply call the sqlite3_bind_blob as,

sqlite3_bind_blob(stmt, 2, array, 6, NULL);

6 is the length of data in bytes, since short occupy 2 bytes, in the above example, it is equal to 6.

To read the stored array from the database you can use sqlite3_column_blob function.

short *myArray = (short*)sqlite3_column_blob(stmt, 2);

You also need to keep a record in database about the length of short array which is 3.

This is a simple way to serialize array types and store in database.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy