Skip to content

Instantly share code, notes, and snippets.

@molpopgen
Created July 24, 2018 22:13
Show Gist options
  • Save molpopgen/aa6225a18466591213880d320748f9bc to your computer and use it in GitHub Desktop.
Save molpopgen/aa6225a18466591213880d320748f9bc to your computer and use it in GitHub Desktop.
Store pickled data from Python into sqlite3 database and retrieve.
import sqlite3
import pickle
import codecs
x = {'a':1,2:'b'}
xp = pickle.dumps(x, -1)
xps = codecs.encode(xp, "base64").decode()
print(xps,type(xps),len(xps))
with sqlite3.connect("test.db") as conn:
conn.execute("create table data (id integer, d text)")
conn.execute("insert into data values ({},\"{}\")".format(1, xps))
conn.commit()
with sqlite3.connect("test.db") as conn:
c = conn.cursor()
c.execute("select d from data where id == 1")
row = c.fetchone()
print(row)
result = pickle.loads(codecs.decode(row[0].encode(),'base64'))
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment