deleteRow
汇总
游标是一个数据访问对象,用于遍历表中的各行上或在表中插入新行。 游标有三种形式: 搜索 、 插入 或 更新 。 游标通常用于读取和更新属性。
旧版本:
游标函数和 Cursor 对象仅供在旧版脚本中使用。 编写或更新脚本时,建议您使用 arcpy.da 模块中的游标。 arcpy.da 游标提供了改进的性能和功能,并支持更新的字段类型和令牌。
语法
deleteRow()
方法
deleteRow(row)
删除数据库中的某一行。 将删除与游标当前所在位置相对应的行。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
row |
The row that will be deleted. |
Row |
insertRow(row)
向数据库中插入新行。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
row |
The row that will be inserted. |
Row |
newRow()
创建空 Row 对象。
返回值
| 数据类型 | 说明 |
|---|---|
|
Row |
新的空 |
next()
返回当前索引中的下一个对象。
返回值
| 数据类型 | 说明 |
|---|---|
|
Object |
当前索引中的下一个对象。 |
reset()
将当前枚举索引(由 next 方法使用)设置回第一个元素。
updateRow(row)
updateRow 方法可用于对更新游标当前所在的行进行更新。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
row |
用于更新游标当前所在位置的行。 |
Row |
代码示例
游标示例 1
使用搜索游标查看行中的字段值。
import arcpy
# Set the workspace
arcpy.env.workspace = "c:/base/data.gdb"
# Create the search cursor
cursor = arcpy.SearchCursor("roads", '"TYPE" <> 4')
# Iterate through the rows in the cursor
for row in cursor:
print("Name: {0}, CFCC code: {1}".format(row.NAME, row.CFCC))
del cursor, row
游标示例 2
使用更新游标更改行中的字段值。
import arcpy
# Set the workspace
arcpy.env.workspace = "c:/base/data.gdb"
# Create the update cursor
cursor = arcpy.UpdateCursor("roads")
# Update the road buffer distance field based on road type.
# Road type is either 1,2,3,4 Distance is in meters.
for row in cursor:
row.setValue("BUFFER_DIST", row.getValue("TYPE") * 100)
cursor.updateRow(row)
# Delete cursor and row objects
del cursor, row
游标示例 3
使用插入游标在表中创建行。
import datetime
import arcpy
# Create insert cursor for table
cursor = arcpy.InsertCursor("c:/base/data.gdb/roads_maint")
# Create 25 new rows. Set default values on distance and CFCC code
for i in range(1000, 1025):
row = cursor.newRow()
row.setValue('rowid', i)
row.setValue('distance', 100)
row.setValue('CFCC', 'A10')
row.setValue('LastInsp', datetime.datetime.now())
cursor.insertRow(row)
# Delete cursor and row objects
del cursor, row