【问题标题】:pandasql OperationalError: near "(": syntax errorpandasql OperationalError:靠近“(”:语法错误
【发布时间】:2021-09-01 15:51:21
【问题描述】:

我在 python 中使用 pandasql,我是 SQL 的新手


query = '''
select row_number() over (order by cumSum) as SegmentOrder
, min(rowLoc) as SegmentStart
, max(rowLoc) - min(rowLoc) as Height
from df_lineLocations
where line = 0
--and CumSum !=0
group by cumSum
'''

df_SegmentLocations  = ps.sqldf(query, locals())
df_SegmentLocations

我尝试删除 row_number() 中的括号,但没有任何反应

【问题讨论】:

  • 能否添加运行时打印到控制台的错误的完整回溯。
  • PandaSQLException: (sqlite3.OperationalError) near "(": 语法错误 [SQL: select row_number over (order by cumSum) as SegmentOrder , min(rowLoc) as SegmentStart , max(rowLoc) - min( rowLoc) as Height from df_lineLocations where line = 0 --and CumSum !=0 group by cumSum ](此错误的背景:sqlalche.me/e/14/e3q8
  • @BarbarosÖzhan 我把 ''' 改成了 """ 但什么也没发生

标签: python sql sqlite sqlalchemy


【解决方案1】:

您只需将您的 SQLite 更新到最新版本,此查询即可运行。

如果您正在使用 Colab,则默认 SQLite 版本是旧版本。每当有需要特殊字符(如 ( 、 * 等)的复合查询时,旧版本就会抛出 OperationalError 。

我已参考此答案将 SQLite 更新到 Colab 上的最新版本: https://stackoverflow.com/a/59429952/13248955

这里是sn-p的代码:

# compiling is the same as before
!curl https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release | tar xz
%cd sqlite/
!./configure
!make sqlite3.c
%cd /content
!npx degit coleifer/pysqlite3 -f
!cp sqlite/sqlite3.[ch] .
!python setup.py build_static build

将解压sqlite最新版本

# check where it is now
!ls build

lib.linux-x86_64-3.7 temp.linux-x86_64-3.7

# !ls build/lib.linux-*version which you got as output in previous step*/pysqlite3/

!ls build/lib.linux-x86_64-3.7/pysqlite3/

dbapi2.py init.py _sqlite3.cpython-37m-x86_64-linux-gnu.so

!ls /usr/lib/ | grep python

输出将是这样的:

python2.7 python3 python3.6 python3.7 python3.8

 # From the list, select python3.7
 !cp build/lib.linux-x86_64-3.7/pysqlite3/_sqlite3.cpython-37m-x86_64-linux-gnu.so/usr/lib/python3.7/lib-dynload/

然后菜单:运行时>重新启动运行时

 import sqlite3
 sqlite3.sqlite_version

'3.37.2' // 将打印出最新的 SQLite 版本

【讨论】:

  • 如果您可以从链接的答案中发布代码 sn-p 那就太好了。我们通常不信任链接,因为它们很容易以您的答案无法控制的方式改变或死亡~
  • 我刚刚用代码 sn-p 编辑了答案。希望对您有所帮助!
猜你喜欢
  • 1970-01-01
  • 2021-05-11
  • 2019-02-07
  • 2020-02-06
  • 2019-01-12
  • 2014-05-17
  • 2021-10-19
  • 2018-07-27
  • 1970-01-01
相关资源
最近更新 更多