向 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",
)
)