from flask import * from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@localhost:3306/file' db = SQLAlchemy(app) class User(db.Model): _table_ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(50),nullable=False) password = db.Column(db.String(50),nullable=False) def query_user(username): with app.app_context(): return User.query.filter(User.username == username).first() def add_user(username, password): with app.app_context(): user = User(username=username, password=password) db.session.add(user) db.session.commit() try: with app.app_context(): db.create_all() except Exception as e: print(f"数据库连接失败: {str(e)}")