import sqlite3
my_path='G:\\My Drive\\testing\\my_db\\my_db.db' # update path
my_conn = sqlite3.connect(my_path)
print("Connected to database successfully")
Using SQLAlchemy ( use any one type connection)
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
my_path='G:\\My Drive\\testing\\my_db\\my_db.db' # update path
my_conn = create_engine("sqlite:///"+ my_path)
We will use read_sql() to create the DataFrame by using the query.from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
my_path='G:\\My Drive\\testing\\my_db\\my_db.db'
my_conn = create_engine("sqlite:///"+ my_path)
import pandas as pd
try:
query="SELECT * FROM student" # query to collect record
df = pd.read_sql(query,my_conn,index_col='id') # create DataFrame
print(df.head()) # Print top 5 rows as sample
df.to_excel('D:\student.xlsx') # create the excel file
except SQLAlchemyError as e:
#print(e)
error = str(e.__dict__['orig'])
print(error)
else:
print("DataFrame created successfully..")
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
my_path='G:\\My Drive\\testing\\my_db\\my_db.db' # update path
my_conn = create_engine("sqlite:///"+ my_path) # connection object
import pandas as pd
try:
df = pd.read_excel('D:\\student.xlsx') # create DataFrame by reading Excel
print(df.head()) # Print top 5 rows as sample
df.to_sql(con=my_conn,name='student3',if_exists='append') # create table.
except SQLAlchemyError as e:
#print(e)
error = str(e.__dict__['orig'])
print(error)
else: # show all records to confirm
r_set=my_conn.execute('SELECT * from student3');
for row in r_set:
print(row)
SQLite to Excel using Colab platform
Author
🎥 Join me live on YouTubePassionate 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.