标签:hello imp nec close hal 不同 port str fetch
使用flask查询mysql数据库,发布http+json对外接口
from flask import Flask
import pymysql
import json
#初始化flask对象
app = Flask(__name__)
#获取数据库链接信息
def get_conn():
return pymysql.connect(
host = ‘192.168.*.*‘,
user = ‘root‘,
password = ‘12345‘,
port = 3306,
db = ‘t3‘,
charset = ‘utf8‘,
autocommit = True
)
#获取数据库查询结果
def query_mysql_data(data_id):
conn = get_conn()
sql = """
select * from u where id = %s
"""
cursor = conn.cursor()
cursor.execute(sql,(data_id,))
conn.close()
return cursor.fetchall()
#接口网址,可通过修改data_id值,获取不同的结果
@app.route(‘/query_data/<data_id>‘)
def hello_world():
return json.dumps(
query_mysql_data()
)
if __name__ == ‘__main__‘:
app.run()
标签:hello imp nec close hal 不同 port str fetch
原文地址:https://www.cnblogs.com/wenchengqingfeng/p/12864768.html