sqlite - peewee.OperationalError 运行查询
问题描述
当我运行以下代码时,我很难确定产生异常的原因。任何帮助,将不胜感激。
请注意,该错误实际上是由 sqlite 报告为 SQL 语法错误。
数据库是 sqlite 版本 3.7.17,我正在使用 peewee 版本 3.13.3 和 Python 3.6.8。操作系统是 Centos 7。
from peewee import *
from covid.models import Confirmed
query = Confirmed.select(
fn.SUM(Confirmed.count)
.over(
order_by=[Confirmed.date],
start=Window.preceding(3)
)
.alias('rsum')
).where(Confirmed.uid == 84036061)
print(query)
for q in query:
print(q)
确认模型是:
class Confirmed(BaseModel):
uid = ForeignKeyField(Domain)
date = DateField()
count = IntegerField()
输出是:
SELECT SUM("t1"."count") OVER (ORDER BY "t1"."date" ROWS 3 PRECEDING) AS "rsum" FROM "confirmed" AS "t1" WHERE ("t1"."uid_id" = 84036061)
Traceback (most recent call last):
File "/home/cp/sw/make/covid/venv/lib64/python3.6/site-packages/peewee.py", line 3099, in execute_sql
cursor.execute(sql, params or ())
sqlite3.OperationalError: near "(": syntax error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "tests/x.py", line 14, in <module>
for q in query:
File "/home/cp/sw/make/covid/venv/lib64/python3.6/site-packages/peewee.py", line 6797, in __iter__
self.execute()
File "/home/cp/sw/make/covid/venv/lib64/python3.6/site-packages/peewee.py", line 1886, in inner
return method(self, database, *args, **kwargs)
File "/home/cp/sw/make/covid/venv/lib64/python3.6/site-packages/peewee.py", line 1957, in execute
return self._execute(database)
File "/home/cp/sw/make/covid/venv/lib64/python3.6/site-packages/peewee.py", line 2129, in _execute
cursor = database.execute(self)
File "/home/cp/sw/make/covid/venv/lib64/python3.6/site-packages/peewee.py", line 3112, in execute
return self.execute_sql(sql, params, commit=commit)
File "/home/cp/sw/make/covid/venv/lib64/python3.6/site-packages/peewee.py", line 3106, in execute_sql
self.commit()
File "/home/cp/sw/make/covid/venv/lib64/python3.6/site-packages/peewee.py", line 2873, in __exit__
reraise(new_type, new_type(exc_value, *exc_args), traceback)
File "/home/cp/sw/make/covid/venv/lib64/python3.6/site-packages/peewee.py", line 183, in reraise
raise value.with_traceback(tb)
File "/home/cp/sw/make/covid/venv/lib64/python3.6/site-packages/peewee.py", line 3099, in execute_sql
cursor.execute(sql, params or ())
peewee.OperationalError: near "(": syntax error
解决方案
正如DinoCoderSaurus正确指出的那样,CentOS 7 预装的 SQLite 版本 3.7.17 不提供 Window 功能支持。
为了其他可能遇到相同问题的人的利益,以下是我解决问题的方法:
为了使用当前的 SQLite 版本,我将它安装在应用程序的 venv 中,方法是:
pip install pysqlite3-binary
(有关 venv 的更多信息,请参阅:https ://docs.python.org/3/library/venv.html )
SQLite 被 CentOS 广泛使用,因此出于向后兼容的原因,替换 /usr/bin 中的系统范围版本并不是一个好主意。
我还应该注意,当我尝试时:
pip install pysqlite3 # Instead of pysqlite3-binary
当我尝试卸载时,安装“成功”完成:
pip uninstall pysqlite3
pip 发出此警告:
警告:跳过 pysqlite3,因为它没有安装。
推荐阅读
- android - 无法解析配置“:app:devDebugRuntimeClasspath”的所有依赖项。对存储库使用不安全的协议,没有
- python - TensorFlow方法中的Jupyter笔记本进度条问题
- sqlite - 加密整个数据库是否比只加密数据更安全?
- javascript - 如何获取地图中特定键的值?
- java - 为什么我们需要在 ViewHolder 构造函数中调用 super(view)?
- laravel - 在不以 JSON 格式保存数据的情况下以可重复方式加载数据的问题 Laravel-Backpack
- amazon-web-services - ETL Glue 增量或增量逻辑
- flutter - 在 StreamBuilder 内构建期间调用的 setState() 或 markNeedsBuild()
- python - 使用带有漂亮汤和 if 语句的 for 循环来填充数据框
- mobile - 在 Mendix Native Mobile App 中触发滚动以自动向下滚动到底部