flask连接数据库
测试的只是主文件和配置文件
主文件
from flask import Flask from 测试链接数据库.config import Config from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config.from_object(Config) # 创建sqlalchemy对象 db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(64), nullable=False) password = db.Column(db.String(64), nullable=False) def __str__(self): return self.name if __name__ == '__main__': db.create_all() app.run()
测试文件(测试文件中可以根据需要分成多个环境进行编写)
class Config(object): DEBUG = True TESTING = False SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:123456789@localhost:3306/mydb" SQLALCHEMY_TRACK_MODIFICATIONS = True