plus2-sqlite-colab-project1)Google Colab runs on a temporary virtual machine whose local files disappear when a runtime disconnects or resets. By placing your SQLite database file (my_db.db) inside a Google Drive folder (plus2-sqlite-colab-project1) and mounting Drive, every table creation and UPDATE query stays saved in Drive—so you can log out, return later, or open another notebook and continue working with the updated records.
plus2-sqlite-colab-project1)settings.pystudent Table (plus2_sqlite_create_table.ipynb)plus2_sqlite_query.ipynb)This project separates shared database configuration from your execution notebooks so multiple scripts inside Google Drive can read and update the same SQLite database file:
| File Name | Purpose |
|---|---|
settings.py |
Stores the Google Drive directory path (/content/drive/MyDrive/plus2-sqlite-colab-project1), database filename (my_db.db), table name (student), dataset URLs, and get_connection() helper. |
plus2_sqlite_create_table.ipynb |
Mounts Google Drive, loads settings.py, creates my_db.db, and populates the student table using your chosen data source. |
plus2_sqlite_query.ipynb |
Connects to my_db.db via settings.py, inspects sample records, adds 5 marks to class = 'Four', and verifies persistence across sessions. |
plus2-sqlite-colab-project1.zip (contains settings.py, plus2_sqlite_create_table.ipynb, and plus2_sqlite_query.ipynb; my_db.db is generated automatically when you run Step 4).plus2-sqlite-colab-project1) 🔝Open your Google Drive (My Drive) and create a new folder named exactly plus2-sqlite-colab-project1. All three project files and the generated my_db.db SQLite database file will live inside this folder.
Extract the downloaded ZIP archive on your computer and upload settings.py, plus2_sqlite_create_table.ipynb, and plus2_sqlite_query.ipynb into your plus2-sqlite-colab-project1 folder in Google Drive.
To run any uploaded .ipynb file in Google Colab, select the notebook in Google Drive, right-click on it, and choose Open with > Google Colaboratory.
settings.py 🔝
Before running the notebooks, open settings.py in Google Drive (right-click settings.py > Open with > Text Editor or view inside Colab's left file explorer) and verify that PROJECT_DIR matches your folder path in Google Drive:
from pathlib import Path
import sqlite3
# Google Drive project directory and SQLite database settings
PROJECT_DIR = Path("/content/drive/MyDrive/plus2-sqlite-colab-project1")
DB_NAME = "my_db.db"
DB_PATH = PROJECT_DIR / DB_NAME
TABLE_NAME = "student"
# Source URLs from plus2net.com/python/pandas-student.php
CSV_URL = "https://www.plus2net.com/python/download/student.csv"
EXCEL_URL = "https://www.plus2net.com/python/download/student.xlsx"
SQL_DUMP_URL = "https://www.plus2net.com/python/download/student.sql"
SQLITE_DB_URL = "https://www.plus2net.com/python/download/my_db.db"
def get_connection():
PROJECT_DIR.mkdir(parents=True, exist_ok=True)
conn = sqlite3.connect(str(DB_PATH))
conn.row_factory = sqlite3.Row
return conn
student Table (plus2_sqlite_create_table.ipynb) 🔝Open plus2_sqlite_create_table.ipynb in Google Colab. First, run the setup cell to mount Google Drive, switch to /content/drive/MyDrive/plus2-sqlite-colab-project1, and load settings.py:
import os
import importlib
import pandas as pd
from google.colab import drive
drive.mount("/content/drive")
os.chdir("/content/drive/MyDrive/plus2-sqlite-colab-project1")
import settings
importlib.reload(settings)
print("Database Path:", settings.DB_PATH)
The notebook provides five ways to populate the 35-row sample student dataset into my_db.db. Run only one of the options below (for example, CSV or Excel using read_csv(), read_excel(), and to_sql()):
# Option A: Create student table from CSV
df_csv = pd.read_csv(settings.CSV_URL)
if "class1" in df_csv.columns and "class" not in df_csv.columns:
df_csv = df_csv.rename(columns={"class1": "class"})
conn = settings.get_connection()
df_csv.to_sql(settings.TABLE_NAME, conn, if_exists="replace", index=False)
conn.close()
Inside plus2_sqlite_create_table.ipynb, you can alternatively run:
student.xlsx with pd.read_excel() and writes to SQLite via to_sql().student.sql and executes the table creation and INSERT script directly in SQLite.my_db.db directly into plus2-sqlite-colab-project1.Finally, run the verification cell at the bottom of plus2_sqlite_create_table.ipynb to confirm my_db.db exists on Google Drive and preview the first 5 rows with SELECT and LIMIT.
plus2_sqlite_query.ipynb) 🔝
Open plus2_sqlite_query.ipynb in Google Colab. After mounting Google Drive and importing settings.py, inspect the existing records using SQLite SELECT and WHERE:
conn = settings.get_connection()
df_before = pd.read_sql_query("SELECT * FROM student WHERE class = 'Four'", conn)
display(df_before)
conn.close()
Next, run the SQLite UPDATE cell to add 5 marks to all students in class = 'Four', display cursor.rowcount (the count of updated rows), and view the updated records:
conn = settings.get_connection()
cursor = conn.cursor()
update_sql = "UPDATE student SET mark = mark + 5 WHERE class = 'Four'"
cursor.execute(update_sql)
conn.commit()
print("Number of records updated:", cursor.rowcount)
df_after = pd.read_sql_query("SELECT * FROM student WHERE class = 'Four'", conn)
display(df_after)
conn.close()
Testing Cross-Session Persistence: Disconnect and delete your Colab runtime (or log out and open Colab the next day), open plus2_sqlite_query.ipynb from plus2-sqlite-colab-project1 in Google Drive, run Step 1 to mount Drive, and run the final check cell (without running the UPDATE cell again). Because my_db.db is saved on Google Drive, the increased marks for class = 'Four' remain intact.
os.chdir("/content/drive/MyDrive/plus2-sqlite-colab-project1") runs after drive.mount("/content/drive") and that settings.py is inside that exact Google Drive folder.UPDATE: Always call conn.commit() after cursor.execute() so SQLite flushes the transaction to my_db.db on Google Drive.my_db.db from the starter ZIP? Excluding my_db.db lets you generate a fresh database file in Step 4 and verify that SQLite creates and updates the file directly inside your Google Drive folder.rowcount.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.