Ⅱ. やり方
1. Flaskをインストールする
pip install flask
pip install flask-mysql
2. サンプルプログラム
from flask import Flask, jsonify
from flaskext.mysql import MySQL
app = Flask(__name__)
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'your_db_user'
app.config['MYSQL_DATABASE_PASSWORD'] = '******'
app.config['MYSQL_DATABASE_DB'] = 'your_db_name'
app.config['MYSQL_DATABASE_HOST'] = '127.0.0.1'
app.config['MYSQL_DATABASE_PORT'] = 3306
mysql.init_app(app)
def ExecuteQuery(sql):
cur = mysql.connect().cursor()
cur.execute(sql)
results = [dict((cur.description[i][0], value)
for i, value in enumerate(row)) for row in cur.fetchall()]
return results
@app.route('/')
def hello_world():
results = ExecuteQuery('select 123 as x from dual')
return jsonify(results)
if __name__ == '__main__':
app.run(debug=True)
実行結果