开始使用免费开始使用

更新多条记录

正如 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
  • 运行解答以打印 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)
编辑并运行代码