You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
920 B

from flask import *
from flask_sqlalchemy import SQLAlchemy
import pymysql
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()
print("数据库连接成功!")
except Exception as e:
print(f"数据库连接失败: {str(e)}")