一,flask-sqlalchemy
安装
pip3 install flask-sqlalchemy
1,在app文件的__init__.py,导入,并实例化
注意:必须在导入蓝图之前,必须导入models
2,初始化:实例.init_app(app)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db=SQLAlchemy()
from .views.user import us
from .models import *
def create_app():
app=Flask(__name__)
app.config.from_object("settings.ProConfig")
app.register_blueprint(us)
db.init_app(app)
return app
3,配置文件
class BaseConfig(object):
SQLALCHEMY_DATABASE_URI="mysql+pymysql://root:123456@127.0.0.1:3306/gl?charset=utf8"
SQLALCHEMY_POOL_SIZE=5
SQLALCHEMY_MAX_OVERFLOW=5
4,创建models.py中的类(表)
注:models.py中的类,继承 实例.Model
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from sqlalchemy import Column
from sqlalchemy import Integer,String
from gl import db
class Users(db.Model):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String(32), index=True, nullable=False)
depart_id = Column(Integer)
5,生成表(使用app上下文)
from gl import db,create_app
app=create_app()
app_ctx=app.app_context() #app_ctx=app/g
with app_ctx: #__enter__通过LocalStark放入Local中
db.create_all() # 调用LocalStack放入Local中获取app,再去app中获取配置
做完上面的几步就可使用orm对数据库进行操作了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from flask import Blueprint
from chun import db
from chun import models
us = Blueprint('us',__name__)
@us.route('/index')
def index():
# 使用SQLAlchemy在数据库中插入一条数据
# db.session.add(models.Users(name='高',depart_id=1))
# db.session.commit()
# db.session.remove()
result = db.session.query(models.Users).all()
print(result)
db.session.remove()
return 'Index'
二,flask-script
安装
pip3 install flask-script
1,runserver
from chun import create_app
from flask_script import Manager
app = create_app()
manager = Manager(app)
if __name__ == '__main__':
manager.run()
然后就可以用runserver来启动项目了
python manage.py runserver
2,自定义命令
1,位置传参
from chun import create_app
from flask_script import Manager
app = create_app()
manager = Manager(app)
@manager.command
def custom(arg):
"""
自定义命令
python manage.py custom 123
:param arg:
:return:
"""
print(arg)
if __name__ == '__main__':
manager.run()
2,关键字传参
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from chun import create_app
from flask_script import Manager
app = create_app()
manager = Manager(app)
@manager.option('-n', '--name', dest='name')
@manager.option('-u', '--url', dest='url')
def cmd(name, url):
"""
自定义命令
执行: python manage.py cmd -n wupeiqi -u http://www.oldboyedu.com
:param name:
:param url:
:return:
"""
rint(name, url)
if __name__ == '__main__':
manager.run()
三,flask-migrate(依赖flask-script)
安装
pip3 install flask-migrate
功能:数据库迁移
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from chun import create_app
from chun import db
app = create_app()
manager = Manager(app)
Migrate(app, db)
"""
# 数据库迁移命名
python manage.py db init
python manage.py db migrate # makemirations
python manage.py db upgrade # migrate
"""
manager.add_command('db', MigrateCommand)
@manager.command
def custom(arg):
"""
自定义命令
python manage.py custom 123
:param arg:
:return:
"""
print(arg)
@manager.option('-n', '--name', dest='name')
@manager.option('-u', '--url', dest='url')
def cmd(name, url):
"""
自定义命令
执行: python manage.py cmd -n wupeiqi -u http://www.oldboyedu.com
:param name:
:param url:
:return:
"""
print(name, url)
if __name__ == '__main__':
# app.run()
manager.run()