-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (59 loc) · 1.75 KB
/
app.py
File metadata and controls
66 lines (59 loc) · 1.75 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
from flask import Flask
import mysql.connector
import json
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, how are you?'
#Get list of product
@app.route('/products')
def get_products():
#connect DB
database = mysql.connector.connect(
host="mysql-flask-app-container",
user="root",
password="123456",
database="ProductManagement"
)
cursor = database.cursor()
cursor.execute("SELECT * FROM tblProduct")
row_header=[x[0] for x in cursor.description]
products = cursor.fetchall()
json_data=[]
for product in products:
json_data.append(dict(zip(row_header,product)))
cursor.close()
return json.dumps(json_data)
@app.route('/initdb')
def initdb():
database = mysql.connector.connect(
host="mysql-flask-app-container",
user="root",
password="123456"
)
cursor = database.cursor()
cursor.execute("DROP DATABASE IF EXISTS ProductManagement")
cursor.execute("CREATE DATABASE ProductManagement")
cursor.close()
return 'init Database'
@app.route('/init_tables')
def init_tables():
database = mysql.connector.connect(
host="mysql-flask-app-container",
user="root",
password="123456",
database="ProductManagement"
)
cursor = database.cursor()
cursor.execute("DROP TABLE IF EXISTS tblProduct")
cursor.execute("""CREATE TABLE tblProduct"""
"""(id INT PRIMARY KEY AUTO_INCREMENT,"""
"""name VARCHAR(255),"""
"""description VARCHAR(255))""")
cursor.close()
return "init_tables"
"""
INSERT INTO tblProduct(name, description)
VALUE ('Macbook Pro M1', 'Please buy'),
('Iphone 13 Promax', 'Please buy now');
"""