Read Sakila database on Google Colab

Hi, currently trying to open this famous database to Google Colab, but it doesn’t open

The code is:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sqlite3

%matplotlib inline

from google.colab import drive
drive.mount('/content/drive')
%cd /content/drive/My Drive/Colab Notebooks/freecodecamp/DataAnalisys/sakila.db

conn = sqlite3.connect('sakila.db')

cursor = conn.cursor()

cursor.execute('SELECT * FROM table')

results = cursor.fetchall()

for row in results:
    print(row)

conn.close()

Any suggestion to get it sorted? is throwing the following error:

OperationalError: near “table”: syntax error

Thanks

Hi @fmorillas85,

One thing I can see is that you should be replacing the word table in the SELECT * FROM table line with the table’s name i.e.

SELECT * FROM myGoogleCollabTable

The word table is a reserved keyword in any SQL language I’ve heard of so it wouldn’t be possible for you to name a table as table.

Hi marcusparsons

Just hit with:

cursor.execute('SELECT * FROM myGoogleCollabTable')

Still throwing an error in that line:

OperationalError: no such table: GoogleCollabTable

You need to replace the name of myGoogleCollabTable with whatever the table name is you’re trying to select data from. There is probably a way to list all of the tables in a database in Sqlite3 if you don’t know the table name.

Just inputting tables’ names but keep throwing the same error… is the first time i try to connect so don’t know

Which lesson or lecture are you working from?

it is freecodecamp exercise 2 on the link below

https://github.com/ine-rmotr-curriculum/FreeCodeCamp-Pandas-Real-Life-Example/blob/master/Exercises_2.ipynb

Here is the command from the code that you linked:

df = pd.read_sql('''
    SELECT
        rental.rental_id, rental.rental_date, rental.return_date,
        customer.last_name AS customer_lastname,
        store.store_id,
        city.city AS rental_store_city,
        film.title AS film_title, film.rental_duration AS film_rental_duration,
        film.rental_rate AS film_rental_rate, film.replacement_cost AS film_replacement_cost,
        film.rating AS film_rating
    FROM rental

You can try rental in place of table

It keeps giving the same error…

Can we see the new code?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sqlite3

%matplotlib inline

from google.colab import drive
drive.mount('/content/drive')
%cd /content/drive/My Drive/Colab Notebooks/freecodecamp/DataAnalisys/sakila.db

conn = sqlite3.connect('sakila.db')

cursor = conn.cursor()

cursor.execute('SELECT * FROM rental')

results = cursor.fetchall()

for row in results:
    print(row)

conn.close()

It works for me but I am getting the file on google colab

%cd /content/drive/My Drive/Colab Notebooks/freecodecamp/DataAnalisys/sakila.db

I would look at this line. You are using “Change directory” but you point to a file. Maybe that is failing and it’s unable to access the DB.

Are you still getting this error?

OperationalError: near “table”: syntax error

Yeah im trying it on Google Colab and runs the following error

OperationalError: no such table: rentaltext

If working, can you share the whole code?

That’s strange, any idea why it says "rentaltext" ?

conn = sqlite3.connect('sakila.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM rental')

results = cursor.fetchall()

for row in results:
    print(row)

conn.close()

Sorry cant tell, keep not executing here…

Can you link to your google colab doc?

Yes, here it is:

Can you share it? (Change permissions to anyone with the link)

Screenshot 2023-09-19 065404
Screenshot 2023-09-19 065821

Just got it doneeeee

Did you see this error:

Mounted at /content/drive
[Errno 20] Not a directory: '/content/drive/My Drive/Colab Notebooks/freecodecamp/DataAnalisys/sakila.db'
/content