-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflaskcrud.py
76 lines (66 loc) · 2 KB
/
flaskcrud.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
from flask import Flask, json, request
from flaskext.mysql import MySQL
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
mysql = MySQL()
users = list(range(100))
# mysql configuratoin
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'CXO'
mysql.init_app(app)
@app.route('/listall')
#GET methodby default
def show():
conn = mysql.connect()
cursor = conn.cursor()
cursor.execute("SELECT * FROM BU")
data = cursor.fetchall()
dataList = []
if data is not None:
for item in data:
dataTempObj = {
'buid' : item[0],
'buname' : item[1],
'buhead' : item[2],
}
dataList.append(dataTempObj)
return json.dumps(dataList)
else:
return 'data error'
@app.route('/delete/<id>')
def delete(id):
conn = mysql.connect()
cursor = conn.cursor()
result = cursor.execute("DELETE FROM BU WHERE buid = %s",int(id))
conn.commit()
conn.close()
if(result):
return json.dumps({'delete':'true'})
else:
return json.dumps({'delete':'false'})
@app.route('/update/',methods=['POST'])
def update():
conn = mysql.connect()
cursor = conn.cursor()
result = cursor.execute("UPDATE BU SET buhead = %s WHERE buid = %s",(request.form['buhead'],request.form['buid']))
conn.commit()
conn.close()
if(result):
return json.dumps({'updated':'true'})
else:
return json.dumps({'updated':'false'})
@app.route('/addnew/',methods=['POST'])
def addnew():
conn = mysql.connect()
cursor = conn.cursor()
result = cursor.execute("INSERT INTO BU values( %s , %s , %s )",(request.form['buid'],request.form['buname'],request.form['buhead']))
conn.commit()
conn.close()
if(result):
return json.dumps({'inserted':'true'})
else:
return json.dumps({'inserted':'false'})
if __name__ == '__main__':
app.run(debug=True)