Skip to content

Instantly share code, notes, and snippets.

@atsuya046
Created December 14, 2013 09:09
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save atsuya046/7957165 to your computer and use it in GitHub Desktop.
Save atsuya046/7957165 to your computer and use it in GitHub Desktop.
create sqlite database file from json file
# -*- coding:utf-8 -*-
import json
import sqlite3
JSON_FILE = "some.json"
DB_FILE = "some.db"
traffic = json.load(open(JSON_FILE))
conn = sqlite3.connect(DB_FILE)
foo = traffic[0]["foo"]
bar = traffic[0]["bar"]
data = [foo, bar]
c = conn.cursor()
c.execute('create table table_name (foo, bar)')
c.execute('insert into table_name values (?,?)', data)
conn.commit()
c.close()
@sakibmas
Copy link

getting following error:

Traceback (most recent call last):
File "./jsonToSqlite.py", line 11, in
foo = traffic[0]["foo"]
KeyError: 0

@Shajeel-Afzal
Copy link

Is something similar available for Java?

@64b2b6d12b
Copy link

data should be a tuple and not a list e.g. data = (foo, bar) in order to be compliant with sqlite3 module.

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