Created
July 24, 2018 22:13
-
-
Save molpopgen/aa6225a18466591213880d320748f9bc to your computer and use it in GitHub Desktop.
Store pickled data from Python into sqlite3 database and retrieve.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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