python - 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:无法设置属性
消除此错误的任何线索都会有所帮助。提前致谢。
解决方案
推荐阅读
- python - 如果超过不同的行索引,则将 numpy 矩阵元素设置为零
- javascript - Javascript:如何在检查用户是否输入特定单词后重定向到一个 url 或另一个 url?
- r - 在 R 中构建预测模型时,使用 step_normalize 函数(tidymodels)是否需要以相同的方式处理测试数据?
- c# - 无法使用 C# 连接到 Unix 套接字
- python - 我的图像处理代码的瓶颈在哪里?
- python - Django 无法使用 Docker 连接到 MariaDB 实例
- nginx - 为 nginx 路由设置适当的代理超时
- angular - 将纯javascript转换为角度组件
- excel - Excel 仅显示负面调查结果
- android - 如何在保持范围内的同时制作 ImageView 的旋转动画?