首页 > 解决方案 > db_create_all() 不工作并在终端上给出错误

问题描述

我正在开发市场应用程序,我需要将一些项目添加到 sqlite3 数据库中,以便我可以检索数据并将其显示在我的网站上。这是 .py 文件的代码:

从烧瓶进口烧瓶,render_template

从 flask_sqlalchemy 导入 SQLAlchemy

应用程序=烧瓶(名称

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db'

db = SQLAlchemy(应用程序)

类项目(db.Model):

id = db.Column(db.Integer(), primary_key = True)

name = db.Column(db.String(length=30), nullable = False, unique = True)

price = db.Column(db.Integer(), nullable = False)

barcode = db.Column(db.String(length = 12), nullable = False, unique = True)

description = db.Column(db.String(length = 1024), nullable = False, unique = True)

@app.route('/')

@app.route('/home')

定义主页():

return render_template('home.html')

@app.route('/market')

定义市场页面():

items = [

    {'id': 1, 'name': 'Phone', 'barcode':'897889788978', 'price':500},

    {'id': 2, 'name': 'Laptop', 'barcode':'789789789456', 'price':1000}

]

return render_template('market.html', items=items)

在终端上:

从 app_name 导入数据库(这有效)

db.create_all() - (这里它给出了一些错误)

回溯(最近一次通话最后):

文件“”,第 1 行,在

文件“C:\Python\Python39\lib\site-packages\flask_sqlalchemy_init _.py ”,第 1033 行,在 create_all

self._execute_for_all_tables(应用程序,绑定,'create_all')

文件“C:\Python\Python39\lib\site-packages\flask_sqlalchemy_init _.py ”,第 1025 行,在

操作(绑定=self.get_engine(应用程序,绑定),**额外)

文件“C:\Python\Python39\lib\site-packages\flask_sqlalchemy_init _.py ”,第 956 行,在 get_engine

返回 connector.get_engine()

文件“C:\Python\Python39\lib\site-packages\flask_sqlalchemy_init _.py ”,第 560 行,在 get_engine

options = self.get_options(sa_url, echo)

文件“C:\Python\Python39\lib\site-packages\flask_sqlalchemy_init _.py ”,第 575 行,在 get_options

self._sa.apply_driver_hacks(self._app,sa_url,选项)

文件“C:\Python\Python39\lib\site-packages\flask_sqlalchemy_init _.py ”,第 908 行,在

apply_driver_hacks

sa_url.database = os.path.join(app.root_path, sa_url.database)

AttributeError:无法设置属性

消除此错误的任何线索都会有所帮助。提前致谢。

标签: pythonsqlite

解决方案


推荐阅读