Persistent SQLite Database Project in Google Colab (plus2-sqlite-colab-project1)

Why Keep Your SQLite Database on Google Drive?

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.

Project Files Overview & ZIP Download 🔝

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.

Step 1: Create Google Drive Folder (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.

Step 2: Upload Project Files & Open in Google Colab 🔝

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.

Step 3: Review and Configure settings.py 🔝

Select settings.py in Google Drive, right-click, and choose Open with Text Editor

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

Step 4: Create Database & 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)
Click Connect to Google Drive in Colab to permit access to Google Drive files

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:

  • Option B (Excel): Loads student.xlsx with pd.read_excel() and writes to SQLite via to_sql().
  • Option C (SQL Dump): Downloads student.sql and executes the table creation and INSERT script directly in SQLite.
  • Option D (Direct SQLite File): Downloads the pre-built my_db.db directly into plus2-sqlite-colab-project1.
  • Option E (Python Dictionary): Builds the DataFrame in memory from a Python dictionary without external downloads.

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.

my_db.db created inside plus2-sqlite-colab-project1 on Google Drive after running plus2_sqlite_create_table.ipynb

Step 5: Query, Update & Verify Persistence Across Sessions (plus2_sqlite_query.ipynb) 🔝

Select notebook in Google Drive, right-click, and choose Open with Google Colaboratory

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.

FAQs & Common Issues 🔝

  • ModuleNotFoundError: No module named 'settings': Make sure 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.
  • Changes do not appear after running UPDATE: Always call conn.commit() after cursor.execute() so SQLite flushes the transaction to my_db.db on Google Drive.
  • Why remove 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.

Related SQLite & Google Colab Tutorials

Managing SQLite Database in Google Colab SQLite to Excel and Vice Versa in Colab




Subscribe to our YouTube Channel here



plus2net.com




Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer