將資料附加到 SQLite 歷史資料表
每週,團隊都會把最新的數位格式摘要寫回共用的 SQLite 歷史資料表。該資料表已經包含上週的快照,而你希望把本週的結果「加在後面」,不要取代既有內容。請使用 write_database 方法,並設定正確的 if_table_exists 參數值。
本週的摘要在 weekly_summary,SQLite 的 URI 在 uri。現有的資料表已為你列印出來,方便你和寫入後的結果比較。
本練習屬於課程
使用 Polars 擴充與最佳化資料管線
練習說明
- 呼叫能把 DataFrame 寫回 SQL 資料表的方法。
- 加上控制如何處理既有資料表的參數。
- 設定它的值,使新列被附加新增。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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",
)
)