Skip to content

Instantly share code, notes, and snippets.

@mcescalante
Last active September 22, 2024 19:27
Show Gist options
  • Save mcescalante/1ba0d88566d903ff64b466d2225f412e to your computer and use it in GitHub Desktop.
Save mcescalante/1ba0d88566d903ff64b466d2225f412e to your computer and use it in GitHub Desktop.
Python SQLite3 INSERT & SELECT for JSON
import sqlite3
import json
conn = sqlite3.connect('test.db')
c = conn.cursor()
# Insert values
c.execute('''insert into data values(?)''', (json.dumps({'test':'test2'}),))
conn.commit()
# Select those values, get them to be json
c.execute('select * from data')
data = c.fetchall()
print data
# print statement will print:
# [(u'{"test": "test2"}',)]
# To load the JSON for python use
for item in data:
print json.loads(item[0])
# print statement will print:
# {u'test': u'test2'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment