forked from ArjunAranetaCodes/MoreCodes-Youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql.py
67 lines (46 loc) · 1.59 KB
/
mysql.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
from flask import Flask, jsonify, request
from flask_mysqldb import MySQL
from flask_cors import CORS
app = Flask(__name__)
app.config['MYSQL_USER'] = 'root1'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'db_tasks'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = MySQL(app)
CORS(app)
@app.route('/api/tasks', methods=['GET'])
def get_all_tasks():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM db_tasks.tasks")
rv = cur.fetchall()
return jsonify(rv)
@app.route('/api/task', methods=['POST'])
def add_task():
cur = mysql.connection.cursor()
title = request.get_json()['title']
cur.execute("INSERT INTO db_tasks.tasks (title) VALUES ('" + str(title) +
"')")
mysql.connection.commit()
result = {'title': title}
return jsonify({'result': result})
@app.route('/api/task/<id>', methods=['PUT'])
def update_task(id):
cur = mysql.connection.cursor()
title = request.get_json()['title']
cur.execute("UPDATE db_tasks.tasks SET title = '" + str(title) +
"' where id = " + id)
mysql.connection.commit()
result = {'title': title}
return jsonify({'result': result})
@app.route('/api/task/<id>', methods=['DELETE'])
def delete_task(id):
cur = mysql.connection.cursor()
response = cur.execute("DELETE FROM db_tasks.tasks where id = " + id)
mysql.connection.commit()
if response > 0:
result = {'message': 'record deleted'}
else:
result = {'message': 'no record found'}
return jsonify({'result': result})
if __name__ == '__main__':
app.run(debug=True)