CommencerCommencez gratuitement

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.

Cet exercice fait partie du cours

<cours>Scaling and Optimizing Data Pipelines with Polars</cours>
Voir le cours

Instructions de l’exercice

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

Exercice interactif pratique

Essayez cet exercice en complétant ce code d’exemple.

# 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",
    )
)
Modifier et exécuter le code