
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 & 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.