Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store array in one column in Sqlite3?

Tags:

c++

sqlite

Is there any way to store an array of integers in one column of table? I want o/p like this:

ident | value                                                            | count  ----------------+------------------------------------------------------------------------------------------------------------------------+------- 563 | [0:10]={"(0,0)","(1,100)","(2,200)","(3,300)","(4,400)","(5,500)"} |    6 

This I have already acheieved through postgres but i want same o/p from sqlite also. Here column value store an array. I tried it through BLOB but it is not working. Somebody told me about serialized way but i am not sure how to do that.

like image 969
SPK Avatar asked Jun 09 '10 11:06

SPK


People also ask

Can I store an array in SQLite?

In relational databases generally, an array is used to store rows with different key columns as an array dimension that means more than one key for a particular key. But in SQLite we cannot directly implement arrays in SQLite.

Can you store an array in a table?

Although an array is one of the most common data types in the world of programming, MySQL actually doesn't support saving an array type directly. You can't create a table column of array type in MySQL. The easiest way store array type data in MySQL is to use the JSON data type.

How do I store an array in SQLite flutter?

You can encode your array as JSON and save as string into a database, then after fetching, decode JSON to a regular map. you need to assign your string value to a particular column. So in the SQLite table, you'll have a column "younameit" with a string value, containing an encoded array.

Can I save a list in SQLite?

Generally, you do this by stringifying the list (with repr()), and then saving the string. On reading the string from the database, use eval() to re-create the list. Be careful, though that you are certain no user-generated data can get into the column, or the eval() is a security risk.


1 Answers

SQLite3 does not support arrays directly. See here the type it supports. Basically, it only does Ints, Floats and Text.

To accomplish what you need, you have to use a custom encoding, or use an FK, i.e. create another table, where each item in the array is stored as a row.

like image 59
Gianni Avatar answered Sep 19 '22 15:09

Gianni