Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to save a list of values into a SQLite column?

Tags:

python

sqlite

I want 3 columns to have 9 different values, like a list in Python. Is it possible? If not in SQLite, then on another database engine?

like image 211
john2x Avatar asked Oct 05 '09 00:10

john2x


1 Answers

You must serialize the list (or other Python object) into a string of bytes, aka "BLOB";-), through your favorite means (marshal is good for lists of elementary values such as numbers or strings &c, cPickle if you want a very general solution, etc), and deserialize it when you fetch it back. Of course, that basically carries the list (or other Python object) as a passive "payload" -- can't meaningfully use it in WHERE clauses, ORDER BY, etc.

Relational databases just don't deal all that well with non-atomic values and would prefer other, normalized alternatives (store the list's items in a different table which includes a "listID" column, put the "listID" in your main table, etc). NON-relational databases, while they typically have limitations wrt relational ones (e.g., no joins), may offer more direct support for your requirement.

Some relational DBs do have non-relational extensions. For example, PostGreSQL supports an array data type (not quite as general as Python's lists -- PgSQL's arrays are intrinsically homogeneous).

like image 101
Alex Martelli Avatar answered Oct 11 '22 20:10

Alex Martelli