-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
76 lines (57 loc) · 1.97 KB
/
model.py
File metadata and controls
76 lines (57 loc) · 1.97 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import sqlite3
class Melon(object):
"""A wrapper object that corresponds to rows in the melons table."""
def __init__(self, id, melon_type, common_name, price, imgurl, flesh_color, rind_color, seedless):
self.id = id
self.melon_type = melon_type
self.common_name = common_name
self.price = price
self.imgurl = imgurl
self.flesh_color = flesh_color
self.rind_color = rind_color
self.seedless = bool(seedless)
def price_str(self):
return "$%.2f"%self.price
def __repr__(self):
return "<Melon: %s, %s, %s>"%(self.id, self.common_name, self.price_str())
class Customer(object):
pass
def connect():
conn = sqlite3.connect("melons.db")
cursor = conn.cursor()
return cursor
def get_melons():
"""Query the database for the first 30 melons, wrap each row in a Melon object"""
cursor = connect()
query = """SELECT id, melon_type, common_name,
price, imgurl,
flesh_color, rind_color, seedless
FROM melons
WHERE imgurl <> ''
LIMIT 30;"""
cursor.execute(query)
melon_rows = cursor.fetchall()
melons = []
for row in melon_rows:
melon = Melon(row[0], row[1], row[2], row[3], row[4], row[5],
row[6], row[7])
melons.append(melon)
print melons
return melons
def get_melon_by_id(id):
"""Query for a specific melon in the database by the primary key"""
cursor = connect()
query = """SELECT id, melon_type, common_name,
price, imgurl,
flesh_color, rind_color, seedless
FROM melons
WHERE id = ?;"""
cursor.execute(query, (id,))
row = cursor.fetchone()
if not row:
return None
melon = Melon(row[0], row[1], row[2], row[3], row[4], row[5],
row[6], row[7])
return melon
def get_customer_by_email(email):
pass