python - 尝试启动 sqlalchemy 数据库但出现错误
问题描述
这显示在我的 vs-studio 终端中:
>>> from app import db #this command is running successful.
>>> db.create_all() # this command not running.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Python\Practice\Flask\env\lib\site-packages\flask_sqlalchemy\__init__.py", line 1039, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "D:\Python\Practice\Flask\env\lib\site-packages\flask_sqlalchemy\__init__.py", line 1031, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "D:\Python\Practice\Flask\env\lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine
return connector.get_engine()
File "D:\Python\Practice\Flask\env\lib\site-packages\flask_sqlalchemy\__init__.py", line 555, in get_engine
options = self.get_options(sa_url, echo)
File "D:\Python\Practice\Flask\env\lib\site-packages\flask_sqlalchemy\__init__.py", line 570, in get_options
self._sa.apply_driver_hacks(self._app, sa_url, options)
File "D:\Python\Practice\Flask\env\lib\site-packages\flask_sqlalchemy\__init__.py", line 883, in apply_driver_hacks
if sa_url.drivername.startswith('mysql'):
AttributeError: 'bool' object has no attribute 'drivername'
解决方案
在您的代码中:
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite://///todo.db" app.config['SQLALCHEMY_DATABASE_URI'] = False app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
删除此行代码:
app.config['SQLALCHEMY_DATABASE_URI'] = False
试试这行代码:
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///todo.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
对于 VSCode 终端:
>>> import app
>>> from app import db
>>> db.create_all()
推荐阅读
- forms - 选择下拉字段的语义 UI 自定义规则验证
- c# - 为什么我的字符串没有被双等号验证,并且字符没有被追加到末尾
- python - Pygame 屏幕循环返回
- php - 发布请求在邮递员中工作,但不在 ajax 中,给出 405 方法未找到错误
- reactjs - Uncaught SyntaxError: Unexpected token '!' reactjs mainchunk.js
- unity3d - Unity UIElemens 在代码中更改 style.cursor
- chisel - Chisel 的开发者指南?
- .net - 将存储库模式与 EF 一起使用时,是否应该跟踪返回的项目?
- cognos-10 - Cognos 将 ParamValue(text_box_input) 转换为数字
- python - choropleth_geojson TypeError:描述符“lower”需要一个“str”对象但收到一个“int”