Skip to content

Instantly share code, notes, and snippets.

@heardy
Last active March 26, 2023 08:25
Show Gist options
  • Save heardy/5618821 to your computer and use it in GitHub Desktop.
Save heardy/5618821 to your computer and use it in GitHub Desktop.
Convert SQLite database to JSON files
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sqlite3 as lite
import sys
con = lite.connect('test.sqlite')
with con:
cur = con.cursor()
cur.execute("CREATE TABLE Cars(Id INT, Name TEXT, Price INT)")
cur.execute("INSERT INTO Cars VALUES(1,'Audi',52642)")
cur.execute("INSERT INTO Cars VALUES(2,'Mercedes',57127)")
cur.execute("INSERT INTO Cars VALUES(3,'Skoda',9000)")
cur.execute("INSERT INTO Cars VALUES(4,'Volvo',29000)")
cur.execute("INSERT INTO Cars VALUES(5,'Bentley',350000)")
cur.execute("INSERT INTO Cars VALUES(6,'Citroen',21000)")
cur.execute("INSERT INTO Cars VALUES(7,'Hummer',41400)")
cur.execute("INSERT INTO Cars VALUES(8,'Volkswagen',21600)")
cur.execute("CREATE TABLE Friends(Id INTEGER PRIMARY KEY, Name TEXT);")
cur.execute("INSERT INTO Friends(Name) VALUES ('Tom');")
cur.execute("INSERT INTO Friends(Name) VALUES ('Rebecca');")
cur.execute("INSERT INTO Friends(Name) VALUES ('Jim');")
cur.execute("INSERT INTO Friends(Name) VALUES ('Robert');")
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sqlite3
import sys
import time
def outputJSONObject(key, val, last):
if val is None:
val = ''
obj = ''
obj += '\t\t"' + str(key).lower() + '" : '
obj += '"' + str(val) + '"'
if last != True:
obj+=','
obj+='\n'
return obj
if len(sys.argv) < 2:
print "Error: Need to pass in SQLite database file to convert"
sys.exit()
db = str(sys.argv[1])
con = sqlite3.connect(db)
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = [item[0] for item in cur.fetchall()]
startTime = time.time()
for table in tables:
cur = con.cursor()
cur.execute("select * from " + table)
col_names = [cn[0] for cn in cur.description]
with open(table + ".json", "w") as f:
f.write("[\n")
items = cur.fetchall()
for i, item in enumerate(items):
if item == None:
break
json = '\t{\n'
for j, col in enumerate(col_names):
last = j == (len(col_names)-1)
json += outputJSONObject(col, item[col], last)
if i != (len(items)-1):
#if i < 10000:
json += '\t},\n'
else:
json += '\t}\n'
f.write(json)
f.write("]")
cur.close()
endTime = time.time()
totalTime = endTime - startTime
print "SQLite to JSON conversion took {0:.2f} seconds".format(totalTime)
@tronghuypbc
Copy link

I got some bug in line 50. In my opinion, item[col] must be item[j]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment