Trying to figure out what the class_field variable is in this query
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])
Solved! Go to Solution.
So, I might be missing something here, but don't you want to print row[0] ?
this example right from ESRI examples Example 4 here:
Prints name of the residential road
Yes, so the sample us printing the value of the field "Name". I thought you wanted to print out the value of the class_field field (that is the road class field) which should be row[0]
which field are you wanting to print?
I now started using this example: not sure if I use a WHERE clause or sql_clause....very new to this so please forgive my ignorance.
But need to insert a WHERE clause : safteysign = Yes OR regulations = No
import arcpy
arcpy.env.workspace = "C:\\Users\\xxx\\AppData\\Roaming\ESRI\\Desktop10.4\\ArcCatalog\\xxx@xx.sde"
fc = "Inspection"
fields = ['safteysign', 'regulations']
# Use ORDER BY sql clause to sort field values
for row in arcpy.da.SearchCursor(
fc, fields, sql_clause=(None, 'regulations')):
print('{0}, {1}'.format(row[0], row[1]))
Example 5B and example 6 in the help documentation show code using the sql_clause parameter. Read the description of the parameters for more detailed information about what each does. Think of a where_clause like a definition query or select by attributes. sql_clause is the extra SQL stuff you can't do in a definition query. In your case, you would just need to use a where_clause.
I think you want to look at sample 5b, and then take at look at SQL reference for query expressions used in ArcGIS—ArcGIS Pro | ArcGIS for Desktop For getting the SQL query set up correctly. AgainI try to build it in Python and pring it out, but also test to nsi sure it is working.
btw, your help doc was pointing to Pro..so mind us too...nit sure if you are using Pro or Desktop, or if the help us much different between the two. Just epwanted to point that out .
Thank you all very much for the help...
getting confused....Blake saying to use Where Clause in ex 4? and you saying use 5b with sql_clause...
if
arcpy.env.workspace = "C:\\Users\\xx\\AppData\\Roaming\ESRI\\Desktop10.4\\ArcCatalog\\xx@xxn.sde"
Is you connection, I'm assuming you are pointing to an SDE connection? But maybe that us sample code again and not your real(sag) code. If using Sde, I think you need the SQL expression that matches you database (SQL, oracle)
Yes I am using SQL and hitting SDE...just need to build the sql_clause then I guess...getting to syntax to work right for something like this is where I am having issues: safteysign = Yes OR regulations = No
The below works but there is no WHERE clause or sql Clause
import arcpy
arcpy.env.workspace = "C:\\Users\\xxk\\AppData\\Roaming\ESRI\\Desktop10.4\\ArcCatalog\\xx@xx.sde"
fc = "Inspection_2"
fields = ['safteysign', 'regulations', 'region_1']
## Use ORDER BY sql clause to sort field values
for row in arcpy.da.SearchCursor(
fc, fields, sql_clause=(None, 'ORDER BY regulations')):
print('{0}, {1}, {2}'.format(row[0], row[1], row[2]))
That was just the example I was referencing....I am trying to build a query that will search at a SDE FC Where Field1 = No or FIeld2 = Yes