Sunday, 20 March 2022

How to save/restore/add elements to Python sets in a Sqlite database?

How to save (and also restore, and add elements to) a set of strings in a Sqlite3 database?

This does not work because sets are not JSON-serializable:

import sqlite3, json
db = sqlite3.connect(':memory:')
db.execute('CREATE TABLE t(id TEXT, myset TEXT);')
s = {'a', 'b', 'c'}
db.execute("INSERT INTO t VALUES (?, ?);", ('1', json.dumps(s)))  
# Error: Object of type set is not JSON serializable

so we can use a list, or a dict with dummy values:

s = list(s)
# or s = {'a':0, 'b':0, 'c': 0}
db.execute("INSERT INTO t VALUES (?, ?);", ('1', json.dumps(s)))  

# RETRIEVE A SET FROM DB
r = db.execute("SELECT myset FROM t WHERE id = '1'").fetchone()
if r is not None:
    s = set(json.loads(r[0]))
    print(s)

Then adding a string element to a set already in the DB is not very elegant:

  • one has to SELECT,
  • retrieve as string,
  • parse the JSON with json.loads,
  • convert from list to set,
  • add an element to the set,
  • convert from set to list (or, as an alternative for these 3 last steps: check if the element is already present in the list, and add it or not to the list)
  • JSONify it with json.dumps,
  • database UPDATE

Is there a more pythonic way to work with sets in a Sqlite database?



from How to save/restore/add elements to Python sets in a Sqlite database?

No comments:

Post a Comment