Articles → Pandas → To_Sql Function In Pandas
To_Sql Function In Pandas
Purpose
Example
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine("sqlite:///c:\sqlite\SchoolDB.db")
df = pd.DataFrame([[3,'Test']])
engine.connect()
# Inserting data in table
df.to_sql("Departments", engine)
print("record inserted")
# Reading data
df = pd.DataFrame()
df = pd.read_sql('select * from Departments', con=engine)
print(df)
Output
Click to Enlarge