-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusing_databases_with_python_multi_table_database_week3.py
More file actions
58 lines (39 loc) · 1.58 KB
/
using_databases_with_python_multi_table_database_week3.py
File metadata and controls
58 lines (39 loc) · 1.58 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#only couple minor changes has to be made within the code
#new table has to be added
CREATE TABLE Genre (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT
);
#genre table has to be updated with new field:
CREATE TABLE Track (
id INTEGER NOT NULL PRIMARY KEY
AUTOINCREMENT UNIQUE,
title TEXT UNIQUE,
album_id INTEGER,
genre_id INTEGER,
len INTEGER, rating INTEGER, count INTEGER
);
#Loops updated
for entry in all:
if ( lookup(entry, 'Track ID') is None ) : continue
name = lookup(entry, 'Name')
artist = lookup(entry, 'Artist')
album = lookup(entry, 'Album')
count = lookup(entry, 'Play Count')
rating = lookup(entry, 'Rating')
length = lookup(entry, 'Total Time')
genre = lookup(entry, 'Genre')
if name is None or artist is None or album is None or genre is None :
continue
print(name, artist, album, count, rating, length, genre)
#new insert statement, so the data can be added to table genre
cur.execute('''INSERT OR IGNORE INTO Genre (name)
VALUES ( ? )''', ( genre, ) )
cur.execute('SELECT id FROM Genre WHERE name = ? ', (genre, ))
genre_id = cur.fetchone()[0]
#Also track insert statement should be updated so genre_id would be added to Track table
cur.execute('''INSERT OR REPLACE INTO Track
(title, album_id, len, rating, count, genre_id)
VALUES ( ?, ?, ?, ?, ?, ? )''',
( name, album_id, length, rating, count, genre_id ) )
#of course before creating the new table we should drop the old one if it already existed. Have fun