Created
May 2, 2017 06:39
-
-
Save spritecodej/dee6b5a85095df8b37213da93c65ca8a to your computer and use it in GitHub Desktop.
Get data as json type in SQLite3
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 as lite | |
def dict_factory(cursor, row): | |
d = {} | |
for idx, col in enumerate(cursor.description): d[col[0]] = row[idx] | |
return d | |
def get_data(db, query): | |
con = lite.connect(db) | |
con.row_factory = dict_factory | |
cur = con.cursor() | |
cur.execute(query) | |
rows = cur.fetchall() | |
return rows | |
if __name__ == "__main__": | |
db = 'test.db' | |
query = "SELECT * FROM Cars;" | |
data = get_data(db, query) | |
for row in data : | |
print (row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment