import pandas as pd
df=pd.read_excel('student.xlsx') # DataFrame created using student file
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)
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.
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.