配置mysql
驱动用pymysql
HOST='127.0.0.1'
PORT=3306
USERNAME='zhang3abc'
PASSWORD='123456'
DB='sitebbsdb'
SQLALCHEMY_DATABASE_URI = f'mysql+pymysql://{USERNAME}:{PASSWORD}@{HOST}:{PORT}/{DB}?charset=utf8'
测试连接
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import text
db = SQLAlchemy(app) # 创建数据库操作的对象
# 测试数据库是否连接成功.
with app.app_context():
with db.engine.connect() as conn:
res=conn.execute(text('select 1')) # 结果: (1,)表示数据库连接成功
print(res.fetchone())
标签:flask,text,app,DB,连接,数据库
From: https://www.cnblogs.com/unity-yancy/p/17232733.html