DataFrame to SQLite database at Colab platform using to_sql()



to_sql(): Data from Pandas DataFrame to SQLite database table #B05


DataFrame to SQlite table in Colab platform using to_sql()
Inside Colab platform (left side ) use the session drive and upload the sample student excel file.
Colab file upload during Runtime

Colab Runtime available of files

Create the DataFrame using read_excel() and the student Excel file.
import pandas as pd
df=pd.read_excel('student.xlsx') # DataFrame created using student file
read_excel() to create DataFrame from Excel at Colab

Create the SQlite database and store the same in session drive.
from sqlalchemy import create_engine,text
from sqlalchemy.exc import SQLAlchemyError
path="sqlite:///my_data.db" # create a SQLite database in google colab session storage
my_conn = create_engine(path)
my_conn=my_conn.connect() # connection is established 
try:    
    result = my_conn.execute(text("select name from sqlite_master where type = 'table'"))
    for row in result:
      print(row) # Display all tables in SQLite database 
except SQLAlchemyError as e:
        error = str(e.__dict__['orig'])
        print(error)   
Creating SQlite database in Colab Platform

Check the table list from SQLite database
df.to_sql(con=my_conn,name='student') # create student table in sqlite database
try:    
    result = my_conn.execute(text("select name from sqlite_master where type = 'table'"))
    for row in result:
      print(row) # List all tables in our SQLite database 
except SQLAlchemyError as e:
        error = str(e.__dict__['orig'])
        print(error) 
('student',)
From student table, display records.
result = my_conn.execute(text("SELECT *  FROM student"))
for row in result:
  print(row) # display row from student table. 
Use conditions to get matching records from SQLite database student table.
result = my_conn.execute(text("SELECT *  FROM student WHERE mark > 80 AND class='Five'"))
for row in result:
  print(row) # display row from student table. 

Questions


Data input and output from Pandas DataFrame Download sample Excel or CSV file or create DataFrame
Pandas DataFrame read_sql read_sql_table insert data
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



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-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer