import arcpy
mxd = arcpy.mapping.MapDocument ("CURRENT")
df = arcpy.mapping.ListDataFrames (mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "Lot_Lines", df)[0]
for lyr in arcpy.mapping.ListLayers(mxd):
tlyr = lyr
dsc = arcpy.Describe(tlyr)
sel_set = dsc.fidSet
if dsc.shapeType == "Polyline":
rows = arcpy.da.SearchCursor(tlyr, "OBJECTID = " + sel_set)
for row in rows:
arcLength = row.ARCLENGTH
shapeLength = row.Shape_Length
if arcLength > 0:
print arcLength
else:
print shapeLength
del row
del rows
The error is in line 32, for row in rows: RuntimeError: An invalid SQL statement was used. I know the select Object ID works, because it changes values depending on the feature selected.
When you switched back to the original cursor type you got no records because your where clause needs to use the IN operator not the = operator. The error using the new arcpy.da cursor is due to the cursor declaration syntax being different. With the arcpy data access (arcpy.da) cursor you have to supply a mandatory field name list prior to defining an optional where clause. Your code has no field list. At a minimum the field list would have to include OBJECTID, ARCLENGTH and Shape_Length. So this code: rows = arcpy.da.SearchCursor(tlyr, "OBJECTID = " + sel_set)should be changed to this code: rows = arcpy.da.SearchCursor(tlyr, ["OID@", "ARCLENGTH", "SHAPE@LENGTH"], "OBJECTID IN (" + sel_set +")")OID@ makes sure you get the correct ObjectID field name reference, regardless of the database. Possibly SHAPE@LENGTH should be Shape_Length, depending on whether you want to access a geodatabase length field or the actual shape geometry length. You also do not have to use field names again after getting the cursor. You can use numbers to represent the fields. So that means the code beyond that line could also change to: rows = arcpy.da.SearchCursor(tlyr, ["OID@", "ARCLENGTH", "SHAPE@LENGTH"], "OBJECTID IN (" + sel_set + ")")
for row in rows:
arcLength = row.[1]
shapeLength = row.[2]
if arcLength > 0:
print arcLength
else:
print shapeLength
del row
del row