/
buildDB_V2.py
177 lines (125 loc) · 5.26 KB
/
buildDB_V2.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
####################################################################################
####################### ----- SI 660 // WINTER 2019----- ########################
############ ----- Mandy Shen, Rahul Bonnerjee, Tammy Nguyen ----- #############
###################### ----- DATABASE DEVELOPMENT ----- ########################
####################################################################################
### --- INFO -----------------------------------------------------------------------
### Creating User & Event databases
## Creating tables in database
## Populating tables in database
####################################################################################
########################### ---------- SETUP ---------- ############################
####################################################################################
### --- IMPORTING MODULES ----------------------------------------------------------
import sqlite3
####################################################################################
########################### ---------- USERS ---------- ############################
####################################################################################
DBNAME = 'communaltable.db'
### --- CREATING DATABASE ----------------------------------------------------------
def create_DB():
## Connect to Database
try:
conn = sqlite3.connect(DBNAME)
except Error as e:
print(e)
cur = conn.cursor()
### -- USERS TABLE -- ###
## Drop table if exists
stmt = """
DROP TABLE IF EXISTS 'users';
"""
cur.execute(stmt)
## Create table
stmt = """
CREATE TABLE 'users' (
'users_id' INTEGER PRIMARY KEY AUTOINCREMENT,
'firstname' TEXT,
'lastname' TEXT,
'email' TEXT,
'diet' TEXT,
'allergy1' TEXT,
'allergy2' TEXT,
'allergy3' TEXT
);
"""
cur.execute(stmt)
conn.commit()
### -- EVENT TABLE -- ###
## Drop table if exists
stmt = """
DROP TABLE IF EXISTS 'events';
"""
cur.execute(stmt)
## Create table
stmt = """
CREATE TABLE 'events' (
'events_id' INTEGER PRIMARY KEY AUTOINCREMENT,
'address1' TEXT,
'address2' TEXT,
'city' TEXT,
'state' TEXT,
'zipcode' INTEGER,
'diet' TEXT,
'ingredient1' TEXT,
'ingredient2' TEXT,
'ingredient3' TEXT,
'description' TEXT,
'allergy1' TEXT,
'allergy2' TEXT,
'allergy3' TEXT,
'donation1' TEXT,
'donation2' TEXT,
'user_id' INTEGER,
'seats' INTEGER,
'mealTime' DATETIME,
'name' TEXT,
'longitude' NUMERIC,
'latitude' NUMERIC,
'price' NUMERIC
);
"""
cur.execute(stmt)
conn.commit()
## Close Database Connection
conn.close()
### --- POPULATING DATABASE --------------------------------------------------------
def populate_DB():
## CONNECT TO DATABASE
try:
conn = sqlite3.connect(DBNAME)
except Error as e:
print(e)
cur = conn.cursor()
### --- USERS TABLE --- ###
users_test = [("Rahul", "Bonnerjee", "rahb@umich.edu", None, None, None, None), ("Mandy", "Shen", "mxshen@umich.edu", "Vegetarian", None, None, None), ("Tammy", "Nguyen", "nguyents@umich.edu", "Vegan", "Peanuts", None, None), ("Eric", "Gilbert", "eegg@umich.edu", None, "Eggs", "Peanuts", None), ("Kentaro", "Toyama", "toyama@umich.edu", None, None, None, None)]
for each in users_test:
data = (None, each[0], each[1], each[2], each[3], each[4], each[5], each[6])
stmt = """
INSERT INTO users
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
"""
cur.execute(stmt, data)
conn.commit()
### --- EVENTS TABLE --- ###
event1 = [("777 N University Ave", None, "Ann Arbor", "MI", 48104, "Vegan", "Tofu", "Carrots", "Cucumbers", "Lemongrass Tofu Banh Mi with Salad Rolls", None, None, None, "Pie", "Beer", 1, 2, "2019-04-23 18:00:00", "Banh Mi First, Pie Competition Later.", 42.279029, -83.739780, 5)]
event2 = [("333 Maynard St", "5th Floor", "Ann Arbor", "MI", 48104, "Vegetarian", "Pasta", "Tomatoes", "Asparagus", "Burst Cherry Tomato Pasta with Roasted Asparagus", "Eggs", "Peanuts", None, "White Wine", "Side Dish", 2, 4, "2019-04-23 20:00:00", "Vegetarian Pasta and Board Game Night!", 42.278424, -83.741623, 6)]
event3 = [("2150 Cram Pl", None, "Ann Arbor", "MI", 48105, None, "BBQ Spicy Pork", "Salad", "Kimchi", "Korean BBQ Spicy Pork with Rice and Side Dishes", None, None, None, "Dessert", "Wine", 3, 3, "2019-04-23 19:30:00", "Korean BBQ and Movie Night!", 42.296946, -83.717452, 7)]
events = event1 + event2 + event3
for each in events:
data = (None, each[0], each[1], each[2], each[3], each[4], each[5], each[6], each[7], each[8], each[9], each[10], each[11], each[12], each[13], each[14], each[15], each[16], each[17], each[18], each[19], each[20], each[21])
stmt = """
INSERT INTO events
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"""
cur.execute(stmt, data)
conn.commit()
## Close Database Connection
conn.close()
####################################################################################
######################## ---------- RUNNING DATA ---------- ########################
####################################################################################
## Create Database
create_DB()
## Populate Database
populate_DB()