示例代码
from flask_sqlalchemy import SQLAlchemy from flask import Flask app=Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = \ 'mysql+pymysql://root:password@127.0.0.1:3306/db2' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db=SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) email = db.Column(db.String(120), unique=True) if __name__ == '__main__': db.create_all() admin = User(username='admin',email='admin@example.com') guest = User(username='guest', email='guest@example.com') db.session.add(admin) db.session.add(guest) db.session.commit()
·安装flask_sqlalchemy,pip install flask-mysqldb