Appending to a SQLite history table
Each week, the team writes the latest digital format summary back to a shared SQLite history table. The table already holds last week's snapshot, and you want this week's results added on top, not replacing what's there. Use the write_database method with the right if_table_exists value.
This week's summary is in weekly_summary, and the SQLite URI is in uri. The existing table is printed for you, so you can compare it with what you write.
This exercise is part of the course
Scaling and Optimizing Data Pipelines with Polars
Exercise instructions
- Call the method that writes a DataFrame back to a SQL table.
- Add the argument that controls how an existing table is handled.
- Set its value so the new rows are appended.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Append this week's summary to the history table
weekly_summary.____(
table_name="weekly_format_history",
connection=uri,
# Append instead of replace or fail
____="____",
)
print("\nHistory after the write:")
print(
pl.read_database_uri(
"SELECT * FROM weekly_format_history",
uri=uri,
engine="adbc",
)
)