安装
pip3 install mysql-connector-python
pip install flask_sqlalchemy
/setting.py
class Configs:
# 数据库信息
MYSQL_USER = ''
MYSQL_PWD = ''
MYSQL_HOST = ''
MYSQL_PORT = ''
DBNAME = ''
# 设置连接数据库路径
SQLALCHEMY_DATABASE_URI = 'mysql+mysqlconnector://{}:{}@{}:{}/{}'.format(
MYSQL_USER, MYSQL_PWD, MYSQL_HOST, MYSQL_PORT, DBNAME)
# 每次请求结束后自动提交数据库中的改动
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
# 禁用SQLAlchemy对追踪对象的修改并且发送信号
SQLALCHEMY_TRACK_MODIFICATIONS = False
# 操作数据库时显示原始SQL语句
SQLALCHEMY_ECHO = True
flask_test.py
导入设置
import setting
app = Flask(__name__)
app.config.from_object(setting.Configs)
app/models/init.py
初始化db
from flask_sqlalchemy import SQLAlchemy
from flask_test import app
db = SQLAlchemy(app)
app/models/app_list.py
from app.models import db
class app_list(db.Model):
__tablename__ = 'HT_DOWNLOADAPP_LIST'
id = db.Column(db.INT, primary_key=True)
platform = db.Column(db.INT, index=True, comment="0: Android 1:iOS")
version = db.Column(db.String(16), index=True)
build = db.Column(db.String(16), index=True)
channel = db.Column(db.String(32))
url = db.Column(db.String(64), nullable=False)
needs = db.Column(db.String(64),index=True)
branch = db.Column(db.String(32),index=True)
packer = db.Column(db.String(32),index=True)
time = db.Column(db.String(32))
dao/init.py
from app.models import db
from app.models.HT_app_list import app_list
#db.create_all()
db.drop_all()
flask_test.py
导入dao包,使得建表语句db.create_all()生效
import dao
标签:sqlalchemy,String,Python,app,db,Column,flask,MYSQL,True
From: https://www.cnblogs.com/QingshanY/p/17369698.html