SQLite Window function not running in google colab notebook

Viewed 464

When I am trying to run below code in google colab:

    #Get required Data
    data8 = pd.read_sql_query('''SELECT  Person.Name, Person.Gender
    FROM M_Cast left JOIN Person USING (PID)
    WHERE PID NOT IN
    (
    SELECT DISTINCT(PID) as PID
    FROM
        (
        SELECT *, LEAD(M_year, 1, 0) OVER (PARTITION BY Actor ORDER BY M_year ASC) AS N_Year
        FROM
            (
            SELECT trim(person.PID) as PID, trim(Person.Name) as Actor, substr(Movie.year,length(Movie.year)-3,4) as M_Year
            FROM Person
            LEFT JOIN M_Cast USING (PID)
            LEFT JOIN Movie USING (MID)
            )
        )
    WHERE (N_Year-M_Year)>3
    )''',con)

print('Output 8: Following is the list of actors never unemployed more than 3 years.(Included actors having single movie):\n')
data8

Colab throws me an error DatabaseError: Execution failed on sql : near "(": syntax error

while its running well on my jupyter notebook

1 Answers

Default sqlite version in Colab is 3.22, but window function is supported in 3.25+. So you need to upgrade it first

!add-apt-repository -y ppa:sergey-dryabzhinsky/packages
!apt update
!apt install sqlite3

Then restart runtime because sqlite is already loaded before.

MENU > Runtime > Restart runtime

Then check that it's a new version

import sqlite3
print(sqlite3.sqlite_version) # 3.33.0
Related