运行报错提示
db.create_all()
Traceback (most recent call last):
File “D:\Program Files\JetBrains\PyCharm 2022.3.2\plugins\python\helpers\pydev\pydevconsole.py”, line 364, in runcode
coro = func()
^^^^^^
File “”, line 1, in
NameError: name ‘db’ is not defined
代码:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(name)
app.config[‘SQLALCHEMY_DATABASE_URL’] = ‘mysql://root:123456@127.0.0.1/flask_news’
db = SQLAlchemy(app)
class User(db.Model):
tablename = 'account_user’
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(32), nullable=False)
@app.route(’/’)
def hello_world(): # put application’s code here
return ‘Hello World!’
if name == ‘main’:
app.run()