-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECdb_setup_v2.py
119 lines (104 loc) · 3.18 KB
/
ECdb_setup_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
import sys
import MySQLdb as mc
import csv
connection = mc.connect (host = "localhost",
user = "root",
passwd = "HiGroup6!",
db = "Easycook")
cur = connection.cursor()
cur.execute("DROP TABLE IF EXISTS Recipe")
cur.execute("DROP TABLE IF EXISTS Ingredient")
cur.execute("DROP TABLE IF EXISTS User")
cur.execute("DROP TABLE IF EXISTS Market")
cur.execute("DROP TABLE IF EXISTS Use_R")
cur.execute("DROP TABLE IF EXISTS Use_I")
cur.execute("DROP TABLE IF EXISTS Favorite_R")
cur.execute("DROP TABLE IF EXISTS Have_I")
cur.execute("DROP TABLE IF EXISTS Sell_I")
sql1 = '''CREATE TABLE IF NOT EXISTS Recipe (
id_R INTEGER AUTO_INCREMENT PRIMARY KEY,
name_R TEXT ,
description TEXT,
calories INTEGER,
fat INTEGER,
protein INTEGER,
sodium INTEGER,
ingredients TEXT,
categories TEXT,
directions TEXT,
rating FLOAT
);'''
cur.execute(sql1)
sql2 = '''CREATE TABLE IF NOT EXISTS Ingredient (
id_I INTEGER AUTO_INCREMENT PRIMARY KEY,
name_I TEXT,
calories INTEGER
);'''
cur.execute(sql2)
sql3 = '''CREATE TABLE IF NOT EXISTS Market (
id_M INTEGER AUTO_INCREMENT PRIMARY KEY,
name_M TEXT,
country_M TEXT
);'''
cur.execute(sql3)
sql4 = '''CREATE TABLE IF NOT EXISTS User (
id_U INTEGER AUTO_INCREMENT PRIMARY KEY,
email TEXT,
nickname TEXT,
password TEXT,
country_U TEXT
);'''
cur.execute(sql4)
sql5 = '''CREATE TABLE IF NOT EXISTS Use_R (
id_R INTEGER,
id_U INTEGER,
rating FLOAT,
comment TEXT,
picture TEXT,
PRIMARY KEY (id_R, id_U)
);'''
cur.execute(sql5)
sql6 = '''CREATE TABLE IF NOT EXISTS Use_I (
id_U INTEGER,
id_I INTEGER,
PRIMARY KEY (id_U, id_I)
);'''
cur.execute(sql6)
sql7 = '''CREATE TABLE IF NOT EXISTS Favorite_R (
id_R INTEGER,
id_U INTEGER,
PRIMARY KEY (id_R, id_U)
);'''
cur.execute(sql7)
sql8 = '''CREATE TABLE IF NOT EXISTS Have_I (
id_R INTEGER,
id_I INTEGER,
PRIMARY KEY (id_R, id_I)
);'''
cur.execute(sql8)
sql9 = '''CREATE TABLE IF NOT EXISTS Sell_I (
id_M INTEGER,
id_I INTEGER,
unit_price FLOAT,
PRIMARY KEY (id_M, id_I)
);'''
cur.execute(sql9)
#Insert data into Recipe
cur.execute('''LOAD DATA LOCAL INFILE 'recipe_mysql.csv'
INTO TABLE `Recipe`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n';''')
#
# with open('recipe_mysql.csv') as f:
# reader = csv.reader(f)
# for row in reader:
# sql = '''INSERT INTO `Recipe`(`name_R`, `description`, `calories`, `fat`, `protein`, `sodium`, `ingredients`, `categories`, `directions`, `rating`)
# VALUES ("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")'''%(row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10])
# try:
# cur.execute(sql)
# connection.commit()
# except:
# connection.rollback()
cur.close()
connection.close()