-
Notifications
You must be signed in to change notification settings - Fork 2
/
crud.py
59 lines (53 loc) · 1.76 KB
/
crud.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
import psycopg2
from flask import Flask, request
import json
app = Flask(__name__) #app initialisation
# CREATE
@app.route('/create', methods=['POST'])
def create():
name = request.json['Name']
conn = psycopg2.connect(database="postgres", user = "postgres", password = "password", host = "127.0.0.1", port = "5432")
cur = conn.cursor()
sql ="insert into data values('%s')"%name
cur.execute(sql)
conn.commit()
conn.close()
response = {'Message':'Success'}
return json.dumps(response)
# READ
@app.route('/read', methods=['GET'])
def read():
conn = psycopg2.connect(database="postgres", user = "postgres", password = "password", host = "127.0.0.1", port = "5432")
cur = conn.cursor()
sql = "select * from data"
cur.execute(sql)
data = cur.fetchone()
conn.close()
response = {'Name':data[0]}
return json.dumps(response)
# UPDATE
@app.route('/update', methods=['PUT'])
def update():
name = request.json['Name']
conn = psycopg2.connect(database="postgres", user = "postgres", password = "password", host = "127.0.0.1", port = "5432")
cur = conn.cursor()
sql = "update data set name = '%s'"%name
cur.execute(sql)
conn.commit()
conn.close()
response = {'Message':'Success'}
return json.dumps(response)
# DELETE
@app.route('/delete', methods=['DELETE'])
def delete():
name = request.json['Name']
conn = psycopg2.connect(database="postgres", user = "postgres", password = "password", host = "127.0.0.1", port = "5432")
cur = conn.cursor()
sql = "delete from data where name='%s'"%name
cur.execute(sql)
conn.commit()
conn.close()
response = {'Message':'Success'}
return json.dumps(response)
if __name__=='__main__':
app.run(threaded=True,host="0.0.0.0",port=8000)