開始使用免費開始

更新多筆紀錄

正如 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
  • 執行解答以列印 resultsrowcount

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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)
編輯並執行程式碼