Skip to content

Pandas Writing

Write to a SQL database

pandas.DataFrame.to_sql

Connect to the db

import sqlalchemy as sql

engine = sql.create_engine("DATABASE_URL")
connection = engine.connect()

Save to the db

result = []
df = pd.DataFrame(result)
df.to_sql(name="table_name",
          con=connection,
          index=False,
          if_exists='replace',  # or append
)

Last update: 2023-04-24