Search Cursor

2922
31
Jump to solution
11-18-2016 12:05 PM
jaykapalczynski
Frequent Contributor

Trying to figure out what the class_field variable is in this query

  • fc is the Feature Class
  • Class_field is WHAT?
  • name_field is the field value?

import arcpy

fc = 'c:/base/data.gdb/roads'
class_field = 'Road Class'
name_field = 'Name'

# Create an expression with proper delimiters
expression = arcpy.AddFieldDelimiters(fc, name_field) + ' = 2'

# Create a search cursor using an SQL expression
with arcpy.da.SearchCursor(fc, [class_field, name_field],
                           where_clause=expression) as cursor:
    for row in cursor:
        # Print the name of the residential road
        print(row[1])
0 Kudos
31 Replies
RebeccaStrauch__GISP
MVP Emeritus

Have you printed out your expression to make sure it is building correctly?  Have you tried to test to make sure it works manually and to make sure there are records being returned?  Maybe your return set is null/empty?

edit: btw, it might be better to include the code you are actaully testing v's just a couple of the example in the help.  Might be something others can see and test in that.

jaykapalczynski
Frequent Contributor

Trying this

import arcpy

arcpy.env.workspace = "C:\\Users\\xx\\AppData\\Roaming\ESRI\\Desktop10.4\\ArcCatalog\\xx@xx.sde"

fc = "Inspection_2"

class_field = 'regulations'
name_field = 'region_1'

# Create an expression with proper delimiters
expression = u'{} = No'.format(arcpy.AddFieldDelimiters(fc, class_field))

# Create a search cursor using an SQL expression
with arcpy.da.SearchCursor(fc, [class_field, name_field],
                           where_clause=expression) as cursor:
    for row in cursor:
        # Print the name of the residential road
        print('{0}, {1}'.format(row[0], row[1]))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Get this error:

Traceback (most recent call last):
File "C:\Users\adminjk\Desktop\PythonSync\Python Scripts\SearchCursor2.py", line 16, in <module>
for row in cursor:
RuntimeError: Attribute column not found [42S22:[Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name 'No'.] [DGIF_TEST.DBO.Inspection_2]

0 Kudos
BlakeTerhune
MVP Regular Contributor

You need to put single quotes around the string. Try simplifying the where_clause to just

"regulations = 'No'‍"‍‍
jaykapalczynski
Frequent Contributor

Think this is working... yea......thank you all for your patience and help...greatly appreciated....now onto figuring out how to write this to a text file.

import arcpy

arcpy.env.workspace = "C:\\Users\\xx\\AppData\\Roaming\ESRI\\Desktop10.4\\ArcCatalog\\xx@xx.sde"

fc = "Inspection_2"
class_field = 'regulations'
name_field = 'region_1'

# Create an expression with proper delimiters
#expression = u'{} = No'.format(arcpy.AddFieldDelimiters(fc, class_field))
expression = "regulations = 'No' OR region_1 = '2'"

# Create a search cursor using an SQL expression
with arcpy.da.SearchCursor(fc, [class_field, name_field],
                           where_clause=expression) as cursor:
    for row in cursor:
        # Print the name of the residential road
        print('{0}, {1}'.format(row[0], row[1]))
0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I agree with Blake, make it simple to start...again test against the SQL manually to get the syntax right since the actual field names might need to include the .DBO etc portion, but maybe not.

work on getting a variable with the correct syntax, then you can replace the "none" in the sql_clause with the expression 

I'm on an iPad right now so nit easy to test

jaykapalczynski
Frequent Contributor

One last thought....how do I push all fields in the FC to output instead of listing all individually?

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

From the search cursor help

Use an asterisk (*) instead of a list of fields if you want to access all fields from the input table (raster and BLOB fields are excluded). However, for faster performance and reliable field order, it is recommended that the list of fields be narrowed to only those that are actually needed.

0 Kudos
jaykapalczynski
Frequent Contributor

Thanks...I forgot the ' '  but it only shows me 2 fields in the python shell...

with arcpy.da.SearchCursor(fc, ['*'],

0 Kudos
jaykapalczynski
Frequent Contributor

Had to modify the print...good to go...THANK YOU ALL

print(row)

jaykapalczynski
Frequent Contributor

I swear last one.......there is a 'u' in front of all the returns when I do *....thoughts?

3, u'{C8339E75-38A8-4D36-B4CF-F837B32C9BBC}', u'2', u'Amherst', u'Mill Creek',

0 Kudos