UpdateCursor

摘要

从要素类或表中更新或删除属性值行。

该游标将数据锁定保留至脚本完成或更新游标对象被删除时。

旧版本:

此函数在 ArcGIS 10.1 中已被 arcpy.da.UpdateCursor 取代,保留此函数仅供旧版脚本使用。 要改进性能、功能和支持更新的字段类型和令牌,请使用 arcpy.da.UpdateCursor

说明

可用于迭代更新游标的方式有两种:for 循环或者 while 循环(通过游标的 next 方法返回下一行)。 如果要使用游标的 next 方法来检索行数为 N 的表中的所有行,则脚本必须调用 next N 次。 在检索到结果集中的最后一行后调用 next 将返回一个 None 类型,在此处用作占位符。

for 循环中遍历 UpdateCursor 函数返回的行。

import arcpy

fc = "c:/data/base.gdb/roads"
field1 = "field1"
field2 = "field2"

cursor = arcpy.UpdateCursor(fc)
for row in cursor:
    # field2 will be equal to field1 multiplied by 3.0
    row.setValue(field2, row.getValue(field1) * 3.0)
    cursor.updateRow(row)

while 循环中遍历 UpdateCursor 函数返回的行。

import arcpy

fc = "c:/data/base.gdb/roads"
field1 = "field1"
field2 = "field2"

cursor = arcpy.UpdateCursor(fc)
row = cursor.next()
while row:
    # field2 will be equal to field1 multiplied by 3.0
    row.setValue(field2, row.getValue(field1) * 3.0)
    cursor.updateRow(row)
    row = cursor.next()

语法

UpdateCursor (dataset, {where_clause}, {spatial_reference}, {fields}, {sort_fields})
参数说明数据类型
dataset

The feature class or table containing the rows to be updated or deleted.

String
where_clause

An expression that limits the rows returned in the cursor. For more information about where clauses and SQL statements, see Introduction to query expressions.

String
spatial_reference

Coordinates are specified in the spatial_reference value provided and converted on the fly to the coordinate system of the dataset.

SpatialReference
fields

A semicolon-delimited string of fields to be included in the cursor. By default, all fields are included.

String
sort_fields

The fields used to sort the rows in the cursor.

Ascending and descending order for each field is denoted by A for ascending and D for descending, using the form "field1 A;field2 B".

String
返回值
数据类型说明
Cursor

返回 Row 对象的 Cursor 对象。

代码示例

UpdateCursor 示例

根据另一个字段值更新要素类中的字段值。

import arcpy

# Create an update cursor for a feature class
rows = arcpy.UpdateCursor("c:/data/base.gdb/roads")

# Update the field used in buffer so the distance is based on the
# road type. Road type is either 1, 2, 3, or 4. Distance is in meters.
for row in rows:
    # Fields from the table can be dynamically accessed from the
    # row object. Here fields named BUFFER_DISTANCE and ROAD_TYPE
    # are used
    row.setValue("BUFFER_DISTANCE", row.getValue("ROAD_TYPE") * 100)
    rows.updateRow(row)

# Delete cursor and row objects to remove locks on the data.
del row
del rows

相关主题