-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-db.py
41 lines (33 loc) · 878 Bytes
/
create-db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from __future__ import print_function
import json
import sqlite3
db = sqlite3.connect("test.db")
cursor = db.cursor()
print("Creating tables")
cursor.execute('''
CREATE TABLE artist(
artistid INTEGER PRIMARY KEY AUTOINCREMENT,
artistname TEXT
);''')
cursor.execute('''
CREATE TABLE track(
trackid INTEGER,
trackname TEXT,
trackartist INTEGER -- Must map to an artist.artistid!
);''')
print("Filling up values")
with open('artists.json') as f:
data = json.loads(f.read())
cursor.executemany('''
insert into artist (artistid, artistname)
values (:artistid, :artistname)
''', data)
with open('tracks.json') as f:
data = json.loads(f.read())
cursor.executemany('''
insert into track (trackid, trackname, trackartist)
values (:trackid, :trackname, :trackartist)
''', data)
db.commit()
cursor.close()
db.close()