簡體   English   中英

谷歌 Colab 中的 sqlite3.OperationalError('near “(”: syntax error')

[英]sqlite3.OperationalError('near “(”: syntax error') in Google Colab

Observing some odd behavior with SQLite 2.6, where the ROW_NUMBER() throws an error only in Google Colab (Python 3.6.9), whereas the code works fine in my local Python 3.6.9 and Python 3.9.1 instances. 你能幫我進一步調試嗎?

代碼

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()

Output

  1. 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
  1. 本地 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)
  1. 本地 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 function 並在 3.25 版中添加了對它的支持。 您可以使用 sqlite3.sqlite_version 或 @forpas 與查詢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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM