pip install flask-sqlalchemy
要链接mysql,仍需要安装flask-mysqldb
pip install flask-mysqldb
对比
django是在setting中配置的
flask中:
# 设置链接数据库的url
app.config['SQLALCHEMY_DATABASE_URL']='mysql://root:mysql@127.0.0.1:3306/test'
# 设置每次请求结束后会自动提交数据库的改动
app.config['SQLACHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = Ture
# 查询时会显示原始sql语句
app.comfig['SQLALCHEMY_ECHO'] = True
db = SQLALchemy(app)
初始化配置
# encoding=utf-8
__author__ = "zhangyue"
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
class Config(object):
"""配置参数"""
# sqlalchemy的配置参数
SQLALCHEMY_DATABASE_URL = "mysql://root:mysql@127.0.0.1:3306/test3"
# 设置SQLALCHEMY自动跟踪数据库
SQLALCHEMY_TRACK_MODIFICATIONS = True
app.config.from_object(Config)
# 创建数据库sqlalchemy工具对象
db = SQLAlchemy(app)
@app.route("/")
def index():
return "index page"
if __name__ == '__main__':
app.run(debug=True)