更新多筆紀錄
正如 Jason 在影片中說明的,只要使用能選到更多筆資料的 where 子句,你就能一次更新多筆紀錄。和插入不同的是,只要你要更新成相同的值,更新多筆與更新單筆的做法完全一樣。現在就來練習吧!
為了方便你作答,本練習會用到的資料表與欄位名稱如下:state_fact(Table)、notes(Column),以及 census_region_name(Column)。
本練習屬於課程
Python 資料庫入門
練習說明
- 建立一個
update陳述式,將state_fact資料表中的notes欄位更新為'The Wild West',並將其儲存為stmt。 - 使用
where子句,篩選state_fact資料表中census_region_name欄位為'West'的紀錄。 - 透過
connection執行stmt_west,並將輸出儲存為results。 - 執行解答以列印
results的rowcount。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Build a statement to update the notes to 'The Wild West': stmt
stmt = update(____).values(____=____)
# Append a where clause to match the West census region records: stmt_west
stmt_west = stmt.____(____ == ____)
# Execute the statement: results
results = connection.execute(____)
# Print rowcount
print(results.rowcount)