Flask连接MySQL数据库时报错RuntimeError: Working outside of application context.
源码:
from flask_sqlalchemy import SQLAlchemy
import pymysql
app = Flask(__name__)
db = SQLAlchemy(app)
class Users(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer,primary_key = True)
username = db.Column(db.String(80),nullable = False)
age = db.Column(db.Integer)
email = db.Column(db.String(120),unique = True)
def __init__(self,username,age,email):
self.username = username
self.age = age
self.email = email
with app.app_context():
return
def __repr__(self):
return '<User:%r>' % self.username
db.create_all()
if __name__ == '__main__':
app.run(debug=True)