美文网首页
flask连接同步数据库

flask连接同步数据库

作者: 陈大水牛 | 来源:发表于2018-04-09 13:19 被阅读0次

    一:

    from flask import Flask

    from exts import db

    import config

    app = Flask(__name__)

    app.config.from_object(config)

    db.init_app(app)

    with app.app_context():

    db.create_all()

    #新建一个article模型,采用models分开的方式

    二:

    from flask_sqlalchemy import SQLAlchemy

    db=SQLAlchemy()

    三:

    from ests import db

    class User(db.Model):

    __tablename__='user'

    id=db.Column(db.Integer,primary_key=True,autoincrement=True)

    telephone =db.Column(db.String(11),nullable=False)

    username=db.Column(db.String(50),nullable=False)

    password =db.Column(db.String(100),nullable=Fa

    四:

    # dialect+driver://username:password@host:port/database

    DIALECT = 'mysql'

    DRIVER = 'mysqldb'

    USERNAME = 'root'

    PASSWORD = '123456'

    HOST = '127.0.0.1'

    PORT = '3306'

    DATABASE = 'db_demo1'

    SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT, DRIVER, USERNAME, PASSWORD, HOST

    , PORT, DATABASE)

    SQLALCHEMY_TRACK_MODIFICATIONS = False

    其他方式

    (manage.py)执行命令  python manage.py db init    migrate  upgrate

    #coding=utf-8

    from flask_script import Manager

    from flask_migrate import Migrate,MigrateCommand

    from zlktqa import app

    from ests import db

    from models import User

    manager= Manager(app)

    migrate =Migrate(app,db)

    manager.add_command('db',MigrateCommand)

    if __name__ =='__main__':

    manager.run()

    相关文章

      网友评论

          本文标题:flask连接同步数据库

          本文链接:https://www.haomeiwen.com/subject/gwoduxtx.html