python sqlite cs50

Solutions on MaxInterview for python sqlite cs50 by the best coders in the world

showing results for - "python sqlite cs50"
Thalia
16 Jul 2018
1import csv
2
3from cs50 import SQL
4
5open("shows.db", "w").close()
6db = SQL("sqlite:///shows.db")
7
8db.execute("CREATE TABLE shows (id INTEGER, title TEXT, PRIMARY KEY(id))")
9db.execute("CREATE TABLE genres (show_id INTEGER, genre TEXT, FOREIGN KEY(show_id) REFERENCES shows(id))")
10
11with open("Favorite TV Shows - Form Responses 1.csv", "r") as file:
12    reader = csv.DictReader(file)
13    for row in reader:
14        title = row["title"].strip().upper()
15
16        id = db.execute("INSERT INTO shows (title) VALUES(?)", title)
17
18        for genre in row["genres"].split(", "):
19            db.execute("INSERT INTO genres (show_id, genre) VALUES(?, ?)", id, genre)
20