Last active
September 22, 2024 19:27
-
-
Save mcescalante/1ba0d88566d903ff64b466d2225f412e to your computer and use it in GitHub Desktop.
Python SQLite3 INSERT & SELECT for JSON
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 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