当前位置 : 主页 > 编程语言 > python >

python flask mysql request 实例

来源:互联网 收集:自由互联 发布时间:2022-07-20
# -*- coding:utf-8 -*- from flask import Flask, jsonify from flask import request #pip install Flask-MySQL #https://github.com/cyberdelia/flask-mysql from flaskext.mysql import MySQL app = Flask(__name__) @app.route('/') def hello_world():


# -*- coding:utf-8 -*-
from flask import Flask, jsonify
from flask import request
#pip install Flask-MySQL
#https://github.com/cyberdelia/flask-mysql
from flaskext.mysql import MySQL

app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello World!'

#http://127.0.0.1:5000/chat?query=你好
@app.route('/chat', methods=['GET'])
def chat():
text = request.args.get('query')

cursor = mysql.get_db().cursor()
cursor.execute("SELECT * FROM table001")
row = cursor.fetchone()
if not row:
print(row[1])

return jsonify({'answer': text})

if __name__ == '__main__':
app.config['MYSQL_DATABASE_USER'] = "mysql001"
app.config['MYSQL_DATABASE_PASSWORD']= "123456"
app.config['MYSQL_DATABASE_HOST'] = "127.0.0.1"
app.config['MYSQL_DATABASE_DB'] = "db001"
mysql = MySQL()
mysql.init_app(app)
app.run(debug=True)


网友评论