更新多条记录
正如 Jason 在视频中所讲,通过使用选择更多记录的 where 子句,您可以一次性更新多条记录。与插入不同,更新多条记录与更新单条记录的方式完全相同(只要您将它们更新为相同的值)。现在就来练习一下吧!
为方便起见,本练习中相关的表与列名称为:state_fact(表)、notes(列)以及 census_region_name(列)。
本练习是课程的一部分
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)