Python写web接口(与mysql交互)
2021-02-21 本文已影响0人
GG_lyf
前言
之前总是写java的crud,写的实在厌烦,因此这次写个Python的(只有查)
1.
数据库
2. Python代码
import pymysql
import json
from flask import Flask
app = Flask(__name__)
@app.route('/index1/<int:id>', methods=['GET'])
def indextest(id):
# inputData = request.json.get('inputData')
data1 = getcontent(id)
return data1
def getcontent(inputData):
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', db='python-mysql') # db:表示数据库名称
cur = conn.cursor()
sql = "select * from book where vId='%d'" % (inputData)
cur.execute(sql)
data = cur.fetchone()
print(data)
result = {'vId': data[0], 'vN': data[1], 'cCnt': data[2]}
return json.dumps(result, ensure_ascii=False, indent=4)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5590)
访问http://127.0.0.1:5590/index1/5