create a api 2b python 2b mysql

Solutions on MaxInterview for create a api 2b python 2b mysql by the best coders in the world

showing results for - "create a api 2b python 2b mysql"
Wallace
23 May 2018
1import pymysql
2from app import app
3from config import mysql
4from flask import jsonify
5from flask import flash, request
6		
7@app.route('/add', methods=['POST'])
8def add_emp():
9	try:
10		_json = request.json
11		_name = _json['name']
12		_email = _json['email']
13		_phone = _json['phone']
14        _address = _json['address']		
15		if _name and _email and _phone and _address and request.method == 'POST':			
16			sqlQuery = "INSERT INTO rest_emp(name, email, phone, address) VALUES(%s, %s, %s, %s, %s)"
17			bindData = (_name, _email, _phone, _address)
18			conn = mysql.connect()
19			cursor = conn.cursor()
20			cursor.execute(sqlQuery, bindData)
21			conn.commit()
22			respone = jsonify('Employee added successfully!')
23			respone.status_code = 200
24			return respone
25		else:
26			return not_found()
27	except Exception as e:
28		print(e)
29	finally:
30		cursor.close() 
31		conn.close()
32		
33@app.route('/emp')
34def emp():
35	try:
36		conn = mysql.connect()
37		cursor = conn.cursor(pymysql.cursors.DictCursor)
38		cursor.execute("SELECT id, name, email, phone, address FROM rest_emp")
39		empRows = cursor.fetchall()
40		respone = jsonify(empRows)
41		respone.status_code = 200
42		return respone
43	except Exception as e:
44		print(e)
45	finally:
46		cursor.close() 
47		conn.close()
48		
49@app.route('/emp/<int:id>')
50def emp(id):
51	try:
52		conn = mysql.connect()
53		cursor = conn.cursor(pymysql.cursors.DictCursor)
54		cursor.execute("SELECT id, name, email, phone, address FROM rest_emp WHERE id =%s", id)
55		empRow = cursor.fetchone()
56		respone = jsonify(empRow)
57		respone.status_code = 200
58		return respone
59	except Exception as e:
60		print(e)
61	finally:
62		cursor.close() 
63		conn.close()
64
65@app.route('/update', methods=['PUT'])
66def update_emp():
67	try:
68		_json = request.json
69		_id = _json['id']
70		_name = _json['name']
71		_email = _json['email']
72		_phone = _json['phone']
73                _address = _json['address']
74                if _name and _email and _phone and _address and _id and request.method == 'PUT':			
75			sqlQuery = "UPDATE rest_emp SET name=%s, email=%s, phone=%s, address=%s WHERE id=%s"
76			bindData = (_name, _email, _phone, _address, _id,)
77			conn = mysql.connect()
78			cursor = conn.cursor()
79			cursor.execute(sqlQuery, bindData)
80			conn.commit()
81			respone = jsonify('Employee updated successfully!')
82			respone.status_code = 200
83			return respone
84		else:
85			return not_found()	
86                except Exception as e:
87		 print(e)
88	        finally:
89		 cursor.close() 
90		 conn.close()
91@app.route('/delete/<int:id>', methods=['DELETE'])
92def delete_emp(id):
93	try:
94		conn = mysql.connect()
95		cursor = conn.cursor()
96		cursor.execute("DELETE FROM rest_emp WHERE id =%s", (id,))
97		conn.commit()
98		respone = jsonify('Employee deleted successfully!')
99		respone.status_code = 200
100		return respone
101	except Exception as e:
102		print(e)
103	finally:
104		cursor.close() 
105		conn.close()
106		
107@app.errorhandler(404)
108def not_found(error=None):
109    message = {
110        'status': 404,
111        'message': 'Record not found: ' + request.url,
112    }
113    respone = jsonify(message)
114    respone.status_code = 404
115    return respone
116		
117if __name__ == "__main__":
118    app.run()