26 lines
770 B
Python
26 lines
770 B
Python
from flask_sqlalchemy import SQLAlchemy
|
|
import sys
|
|
|
|
db = SQLAlchemy()
|
|
|
|
|
|
def init_db(app):
|
|
"""初始化数据库"""
|
|
db.init_app(app)
|
|
|
|
try:
|
|
with app.app_context():
|
|
# 测试数据库连接
|
|
result = db.session.execute(db.text('SELECT 1'))
|
|
print("✅ 数据库连接成功")
|
|
|
|
# 由于表已存在,我们只需要确保模型与数据库同步
|
|
# 不需要重新创建表
|
|
print("✅ 数据库初始化完成")
|
|
|
|
except Exception as e:
|
|
print(f"❌ 数据库初始化失败: {e}")
|
|
print("请检查数据库配置和网络连接")
|
|
# 在开发环境中不退出,允许继续运行
|
|
print("⚠️ 继续运行,但可能会有数据库相关问题")
|