forked from jesstess/oreilly-intermediate-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_categories.py
More file actions
30 lines (22 loc) · 805 Bytes
/
game_categories.py
File metadata and controls
30 lines (22 loc) · 805 Bytes
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
# Goal:
# Select a random game and print all of the categories from that game.
# SQL to get a random game:
# SELECT game FROM category ORDER BY RANDOM() LIMIT 1
# SQL to get the categories for a particular game:
# """SELECT name, round FROM category
# WHERE game=%d ORDER BY round""" % (game_id,)
import sqlite3
connection = sqlite3.connect('jeopardy.db')
cursor = connection.cursor()
# Get a random game.
cursor.execute("SELECT game FROM category ORDER BY RANDOM() LIMIT 1")
results = cursor.fetchall()
game_id = results[0][0]
print("Categories for game #%d:" % (game_id,))
# Get the categories for that game.
query = """SELECT name, round FROM category
WHERE game=%d ORDER BY round""" % (game_id,)
cursor.execute(query)
results = cursor.fetchall()
# TODO: process results.
connection.close()