-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomework_cars3b.py
More file actions
33 lines (23 loc) · 1 KB
/
homework_cars3b.py
File metadata and controls
33 lines (23 loc) · 1 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
# JOIN + SQLite Functions
"""Output the car's make and model on one line, the quantity on another line,
and then the order count on the next line. The latter is a bit difficult,
but please try it first before looking at the code. **Remember: Google-it-first!**"""
import sqlite3
with sqlite3.connect("cars.db") as connection:
c = connection.cursor()
# retrieve data
c.execute("SELECT * FROM cars")
# fetchall() retrieves all records from the query
rows = c.fetchall()
# output the rows to the screen, row by row
for r in rows:
# output the car make, model and quantity to screen
print
print r[0], r[1], "\n", r[2]
# retrieve order_date for the current car make and model
c.execute("SELECT count(order_date) FROM orders WHERE make=? and model=?",
(r[0], r[1]))
# fetchone() retrieves one record from the query
order_count = c.fetchone()[0]
# output the order count to the screen
print order_count