sqlite3.OperationalError('near "(": syntax error') 在 Google Colab 中
sqlite3.OperationalError('near "(": syntax error') in Google Colab
观察到 SQLite 2.6 的一些奇怪行为,其中 ROW_NUMBER()
仅在 Google Colab(Python 3.6.9)中抛出错误,而代码在我的本地 Python 3.6.9 和 Python 3.9.1 实例中工作正常。你能帮我进一步调试吗?
代码
import sqlite3, sys
try:
print('Py.version : ' + (sys.version))
print('sqlite3.version : ' + (sqlite3.version))
print('sqlite3.sqlite_version : ' + (sqlite3.sqlite_version)+'\n')
conn = sqlite3.connect(':memory:')
conn.execute('''CREATE TABLE team_data(team text, total_goals integer);''')
conn.commit()
conn.execute("INSERT INTO team_data VALUES('Real Madrid', 53);")
conn.execute("INSERT INTO team_data VALUES('Barcelona', 47);")
conn.commit()
sql='''
SELECT
team,
ROW_NUMBER () OVER (
ORDER BY total_goals
) RowNum
FROM
team_data
'''
print('### DB Output ###')
cursor = conn.execute(sql)
for row in cursor:
print(row)
except Exception as e:
print('ERROR : ' + str(e))
finally:
conn.close()
输出
- Google Colab(ROW_NUMBER() 导致 SQL 失败 ):
Py.version : 3.6.9 (default, Oct 8 2020, 12:12:24) [GCC 8.4.0]
sqlite3.version : 2.6.0
sqlite3.sqlite_version : 3.22.0
### DB Output ###
ERROR : near "(": syntax error
- 本地 Python 3.6.9(成功):
Py.version : 3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 14:00:49) [MSC v.1915 64 bit (AMD64)]
sqlite3.version : 2.6.0
sqlite3.sqlite_version : 3.33.0
### DB Output ###
('Barcelona', 1)
('Real Madrid', 2)
- 本地 Python 3.9.1(成功):
Py.version : 3.9.1 (default, Dec 11 2020, 09:29:25) [MSC v.1916 64 bit (AMD64)]
sqlite3.version : 2.6.0
sqlite3.sqlite_version : 3.33.0
### DB Output ###
('Barcelona', 1)
('Real Madrid', 2)
注意:以上 SQL 和代码已简化,仅用于错误重现目的
有问题的查询是一个 window 函数,并且在版本 3.25 中添加了对该函数的支持。您可以使用 sqlite3.sqlite_version 或与查询 select sqlite_version()
.
共享的@forpas 检查库(与包相对)版本
您可以升级您的sqlite版本。使用此代码。
!add-apt-repository -y ppa:sergey-dryabzhinsky/packages
!apt update
!apt install sqlite3
# MENU: Runtime > Restart runtime
import sqlite3
sqlite3.sqlite_version # '3.33.0'
观察到 SQLite 2.6 的一些奇怪行为,其中 ROW_NUMBER()
仅在 Google Colab(Python 3.6.9)中抛出错误,而代码在我的本地 Python 3.6.9 和 Python 3.9.1 实例中工作正常。你能帮我进一步调试吗?
代码
import sqlite3, sys
try:
print('Py.version : ' + (sys.version))
print('sqlite3.version : ' + (sqlite3.version))
print('sqlite3.sqlite_version : ' + (sqlite3.sqlite_version)+'\n')
conn = sqlite3.connect(':memory:')
conn.execute('''CREATE TABLE team_data(team text, total_goals integer);''')
conn.commit()
conn.execute("INSERT INTO team_data VALUES('Real Madrid', 53);")
conn.execute("INSERT INTO team_data VALUES('Barcelona', 47);")
conn.commit()
sql='''
SELECT
team,
ROW_NUMBER () OVER (
ORDER BY total_goals
) RowNum
FROM
team_data
'''
print('### DB Output ###')
cursor = conn.execute(sql)
for row in cursor:
print(row)
except Exception as e:
print('ERROR : ' + str(e))
finally:
conn.close()
输出
- Google Colab(ROW_NUMBER() 导致 SQL 失败 ):
Py.version : 3.6.9 (default, Oct 8 2020, 12:12:24) [GCC 8.4.0]
sqlite3.version : 2.6.0
sqlite3.sqlite_version : 3.22.0
### DB Output ###
ERROR : near "(": syntax error
- 本地 Python 3.6.9(成功):
Py.version : 3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 14:00:49) [MSC v.1915 64 bit (AMD64)]
sqlite3.version : 2.6.0
sqlite3.sqlite_version : 3.33.0
### DB Output ###
('Barcelona', 1)
('Real Madrid', 2)
- 本地 Python 3.9.1(成功):
Py.version : 3.9.1 (default, Dec 11 2020, 09:29:25) [MSC v.1916 64 bit (AMD64)]
sqlite3.version : 2.6.0
sqlite3.sqlite_version : 3.33.0
### DB Output ###
('Barcelona', 1)
('Real Madrid', 2)
注意:以上 SQL 和代码已简化,仅用于错误重现目的
有问题的查询是一个 window 函数,并且在版本 3.25 中添加了对该函数的支持。您可以使用 sqlite3.sqlite_version 或与查询 select sqlite_version()
.
您可以升级您的sqlite版本。使用此代码。
!add-apt-repository -y ppa:sergey-dryabzhinsky/packages
!apt update
!apt install sqlite3
# MENU: Runtime > Restart runtime
import sqlite3
sqlite3.sqlite_version # '3.33.0'