更新單筆紀錄
update 陳述式和 insert 很類似。例如,你可以如下更新 employees 資料表中的所有薪資:
stmt = update(employees).values(wage=100.00)
update 通常也會搭配 where 子句,來決定要更新哪些資料。比如,只更新 ID 為 15 的員工紀錄,可以在前述陳述式後面加上:
stmt = stmt.where(employees.id == 15)
你會在這裡使用 FIPS 州別代碼,這是美國政府用來識別各州與部分屬地的代碼。
為了方便你完成本題,這個練習中相關的資料表與欄位名稱為:state_fact(Table)、name(Column)、fips_state(Column)。
本練習屬於課程
Python 資料庫入門
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Build a select statement: select_stmt
select_stmt = select([____]).where(____ == ____)
# Execute select_stmt and fetch the results
results = connection.____(____).____()
# Print the results of executing the select_stmt
print(____)
# Print the FIPS code for the first row of the result
print(results[0]['___'])