Not Able to Pass Where Clause in SearchCursor in ArcPy
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
Can you please take a look at this snippet and let me know why I am not able to properly pass the Where Clause ('"[NAME_1]" = Ohio') in SearchCursor?
import arcpy
from arcpy import env
def unique_values(table , field):
with arcpy.da.SearchCursor(table, [field], '"[NAME_1]" = Ohio') as cursor:
return sorted(row[0] for row in cursor)
uniques = unique_values(r'C:arcgisArcTutorAAAsrcUSA.shp' , 'NAME_2')
for unique in uniques:
print (unique)
I am getting this error on runtime
Traceback (most recent call last):
File "<module1>", line 8, in <module>
File "<module1>", line 7, in unique_values
File "<module1>", line 7, in <setcomp>
RuntimeError: Unspecified error
Same error on this format as well
with arcpy.da.SearchCursor(table, [field], '"NAME_1" = Ohio') as cursor:
arcpy cursor select-by-attribute
 |Â
show 1 more comment
up vote
1
down vote
favorite
Can you please take a look at this snippet and let me know why I am not able to properly pass the Where Clause ('"[NAME_1]" = Ohio') in SearchCursor?
import arcpy
from arcpy import env
def unique_values(table , field):
with arcpy.da.SearchCursor(table, [field], '"[NAME_1]" = Ohio') as cursor:
return sorted(row[0] for row in cursor)
uniques = unique_values(r'C:arcgisArcTutorAAAsrcUSA.shp' , 'NAME_2')
for unique in uniques:
print (unique)
I am getting this error on runtime
Traceback (most recent call last):
File "<module1>", line 8, in <module>
File "<module1>", line 7, in unique_values
File "<module1>", line 7, in <setcomp>
RuntimeError: Unspecified error
Same error on this format as well
with arcpy.da.SearchCursor(table, [field], '"NAME_1" = Ohio') as cursor:
arcpy cursor select-by-attribute
Skip the bracers, use "NAME_1 = 'Ohio'" (note the use of double and single quotes). If you're only using one field it doesn't need to be a list, use with arcpy.da.SearchCursor(table, field, "NAME_1 = 'Ohio'") as cursor: and you should reduce your problems. What's the next line about? You're making a dictionary but it's not, there's no key:value pairs, surely you want to do sorted([row[0] for row in cursor])?
â Michael Stimson
27 mins ago
@MichaelStimson, OP is using a shapefile. Therefore, the double quotes are necessary.
â Fezterâ¦
23 mins ago
1
@Fezter, no they're not. Try it and see.
â Michael Stimson
22 mins ago
1
This is how it shoul like for shapefile "NAME_1" = 'Ohio'
â FelixIP
21 mins ago
1
I understand your aversion to shapefiles @Fezter, I have found that no quote or bracers are required for any of the common storage types when defining a cursor. It may be that some obscure enterprise database needs them but I've not found one yet that complained.
â Michael Stimson
19 mins ago
 |Â
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Can you please take a look at this snippet and let me know why I am not able to properly pass the Where Clause ('"[NAME_1]" = Ohio') in SearchCursor?
import arcpy
from arcpy import env
def unique_values(table , field):
with arcpy.da.SearchCursor(table, [field], '"[NAME_1]" = Ohio') as cursor:
return sorted(row[0] for row in cursor)
uniques = unique_values(r'C:arcgisArcTutorAAAsrcUSA.shp' , 'NAME_2')
for unique in uniques:
print (unique)
I am getting this error on runtime
Traceback (most recent call last):
File "<module1>", line 8, in <module>
File "<module1>", line 7, in unique_values
File "<module1>", line 7, in <setcomp>
RuntimeError: Unspecified error
Same error on this format as well
with arcpy.da.SearchCursor(table, [field], '"NAME_1" = Ohio') as cursor:
arcpy cursor select-by-attribute
Can you please take a look at this snippet and let me know why I am not able to properly pass the Where Clause ('"[NAME_1]" = Ohio') in SearchCursor?
import arcpy
from arcpy import env
def unique_values(table , field):
with arcpy.da.SearchCursor(table, [field], '"[NAME_1]" = Ohio') as cursor:
return sorted(row[0] for row in cursor)
uniques = unique_values(r'C:arcgisArcTutorAAAsrcUSA.shp' , 'NAME_2')
for unique in uniques:
print (unique)
I am getting this error on runtime
Traceback (most recent call last):
File "<module1>", line 8, in <module>
File "<module1>", line 7, in unique_values
File "<module1>", line 7, in <setcomp>
RuntimeError: Unspecified error
Same error on this format as well
with arcpy.da.SearchCursor(table, [field], '"NAME_1" = Ohio') as cursor:
arcpy cursor select-by-attribute
arcpy cursor select-by-attribute
edited 55 mins ago
asked 1 hour ago
Mona Coder
21819
21819
Skip the bracers, use "NAME_1 = 'Ohio'" (note the use of double and single quotes). If you're only using one field it doesn't need to be a list, use with arcpy.da.SearchCursor(table, field, "NAME_1 = 'Ohio'") as cursor: and you should reduce your problems. What's the next line about? You're making a dictionary but it's not, there's no key:value pairs, surely you want to do sorted([row[0] for row in cursor])?
â Michael Stimson
27 mins ago
@MichaelStimson, OP is using a shapefile. Therefore, the double quotes are necessary.
â Fezterâ¦
23 mins ago
1
@Fezter, no they're not. Try it and see.
â Michael Stimson
22 mins ago
1
This is how it shoul like for shapefile "NAME_1" = 'Ohio'
â FelixIP
21 mins ago
1
I understand your aversion to shapefiles @Fezter, I have found that no quote or bracers are required for any of the common storage types when defining a cursor. It may be that some obscure enterprise database needs them but I've not found one yet that complained.
â Michael Stimson
19 mins ago
 |Â
show 1 more comment
Skip the bracers, use "NAME_1 = 'Ohio'" (note the use of double and single quotes). If you're only using one field it doesn't need to be a list, use with arcpy.da.SearchCursor(table, field, "NAME_1 = 'Ohio'") as cursor: and you should reduce your problems. What's the next line about? You're making a dictionary but it's not, there's no key:value pairs, surely you want to do sorted([row[0] for row in cursor])?
â Michael Stimson
27 mins ago
@MichaelStimson, OP is using a shapefile. Therefore, the double quotes are necessary.
â Fezterâ¦
23 mins ago
1
@Fezter, no they're not. Try it and see.
â Michael Stimson
22 mins ago
1
This is how it shoul like for shapefile "NAME_1" = 'Ohio'
â FelixIP
21 mins ago
1
I understand your aversion to shapefiles @Fezter, I have found that no quote or bracers are required for any of the common storage types when defining a cursor. It may be that some obscure enterprise database needs them but I've not found one yet that complained.
â Michael Stimson
19 mins ago
Skip the bracers, use "NAME_1 = 'Ohio'" (note the use of double and single quotes). If you're only using one field it doesn't need to be a list, use with arcpy.da.SearchCursor(table, field, "NAME_1 = 'Ohio'") as cursor: and you should reduce your problems. What's the next line about? You're making a dictionary but it's not, there's no key:value pairs, surely you want to do sorted([row[0] for row in cursor])?
â Michael Stimson
27 mins ago
Skip the bracers, use "NAME_1 = 'Ohio'" (note the use of double and single quotes). If you're only using one field it doesn't need to be a list, use with arcpy.da.SearchCursor(table, field, "NAME_1 = 'Ohio'") as cursor: and you should reduce your problems. What's the next line about? You're making a dictionary but it's not, there's no key:value pairs, surely you want to do sorted([row[0] for row in cursor])?
â Michael Stimson
27 mins ago
@MichaelStimson, OP is using a shapefile. Therefore, the double quotes are necessary.
â Fezterâ¦
23 mins ago
@MichaelStimson, OP is using a shapefile. Therefore, the double quotes are necessary.
â Fezterâ¦
23 mins ago
1
1
@Fezter, no they're not. Try it and see.
â Michael Stimson
22 mins ago
@Fezter, no they're not. Try it and see.
â Michael Stimson
22 mins ago
1
1
This is how it shoul like for shapefile "NAME_1" = 'Ohio'
â FelixIP
21 mins ago
This is how it shoul like for shapefile "NAME_1" = 'Ohio'
â FelixIP
21 mins ago
1
1
I understand your aversion to shapefiles @Fezter, I have found that no quote or bracers are required for any of the common storage types when defining a cursor. It may be that some obscure enterprise database needs them but I've not found one yet that complained.
â Michael Stimson
19 mins ago
I understand your aversion to shapefiles @Fezter, I have found that no quote or bracers are required for any of the common storage types when defining a cursor. It may be that some obscure enterprise database needs them but I've not found one yet that complained.
â Michael Stimson
19 mins ago
 |Â
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
2
down vote
Use AddFieldDelimiters:
The field delimiters used in an SQL expression differ depending on the
format of the queried data. For instance, file geodatabases and
shapefiles use double quotation marks (" "), personal geodatabases use
square brackets ([ ]), and enterprise geodatabases don't use field
delimiters. The function can take away the guess work in ensuring that
the field delimiters used with your SQL expression are the correct
ones.
with single quotes around Ohio and three double quotes surrounding everything:
sql = """0 = 'Ohio'""".format(arcpy.AddFieldDelimiters(table,'NAME_1'))
with arcpy.da.SearchCursor(table,field,sql) as cursor:
...
The problem isn't with the field as specified but with the value, Felix stated it most succinctly, the SQL definitely needs a single quote delimiter for a string literal, as the OP had the query stated the SQL was looking to compare NAME_1 field with a field called Ohio and not with the string literal 'Ohio'.
â Michael Stimson
9 mins ago
add a comment |Â
up vote
1
down vote
I wasn't able to replicate your error, but try this:
import arcpy
expression = "NAME_1 = 'Ohio'"
def unique_values(table , field):
with arcpy.da.SearchCursor(table, [field], where_clause=expression) as cursor:
return sorted(row[0] for row in cursor)
uniques = unique_values(r'C:arcgisArcTutorAAAsrcUSA.shp' , 'NAME_2')
for unique in uniques:
print (unique)
The SearchCursor where_clause parameter takes a SQL string as its input. There are more examples here
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Use AddFieldDelimiters:
The field delimiters used in an SQL expression differ depending on the
format of the queried data. For instance, file geodatabases and
shapefiles use double quotation marks (" "), personal geodatabases use
square brackets ([ ]), and enterprise geodatabases don't use field
delimiters. The function can take away the guess work in ensuring that
the field delimiters used with your SQL expression are the correct
ones.
with single quotes around Ohio and three double quotes surrounding everything:
sql = """0 = 'Ohio'""".format(arcpy.AddFieldDelimiters(table,'NAME_1'))
with arcpy.da.SearchCursor(table,field,sql) as cursor:
...
The problem isn't with the field as specified but with the value, Felix stated it most succinctly, the SQL definitely needs a single quote delimiter for a string literal, as the OP had the query stated the SQL was looking to compare NAME_1 field with a field called Ohio and not with the string literal 'Ohio'.
â Michael Stimson
9 mins ago
add a comment |Â
up vote
2
down vote
Use AddFieldDelimiters:
The field delimiters used in an SQL expression differ depending on the
format of the queried data. For instance, file geodatabases and
shapefiles use double quotation marks (" "), personal geodatabases use
square brackets ([ ]), and enterprise geodatabases don't use field
delimiters. The function can take away the guess work in ensuring that
the field delimiters used with your SQL expression are the correct
ones.
with single quotes around Ohio and three double quotes surrounding everything:
sql = """0 = 'Ohio'""".format(arcpy.AddFieldDelimiters(table,'NAME_1'))
with arcpy.da.SearchCursor(table,field,sql) as cursor:
...
The problem isn't with the field as specified but with the value, Felix stated it most succinctly, the SQL definitely needs a single quote delimiter for a string literal, as the OP had the query stated the SQL was looking to compare NAME_1 field with a field called Ohio and not with the string literal 'Ohio'.
â Michael Stimson
9 mins ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Use AddFieldDelimiters:
The field delimiters used in an SQL expression differ depending on the
format of the queried data. For instance, file geodatabases and
shapefiles use double quotation marks (" "), personal geodatabases use
square brackets ([ ]), and enterprise geodatabases don't use field
delimiters. The function can take away the guess work in ensuring that
the field delimiters used with your SQL expression are the correct
ones.
with single quotes around Ohio and three double quotes surrounding everything:
sql = """0 = 'Ohio'""".format(arcpy.AddFieldDelimiters(table,'NAME_1'))
with arcpy.da.SearchCursor(table,field,sql) as cursor:
...
Use AddFieldDelimiters:
The field delimiters used in an SQL expression differ depending on the
format of the queried data. For instance, file geodatabases and
shapefiles use double quotation marks (" "), personal geodatabases use
square brackets ([ ]), and enterprise geodatabases don't use field
delimiters. The function can take away the guess work in ensuring that
the field delimiters used with your SQL expression are the correct
ones.
with single quotes around Ohio and three double quotes surrounding everything:
sql = """0 = 'Ohio'""".format(arcpy.AddFieldDelimiters(table,'NAME_1'))
with arcpy.da.SearchCursor(table,field,sql) as cursor:
...
edited 35 secs ago
answered 18 mins ago
BERA
12.3k41637
12.3k41637
The problem isn't with the field as specified but with the value, Felix stated it most succinctly, the SQL definitely needs a single quote delimiter for a string literal, as the OP had the query stated the SQL was looking to compare NAME_1 field with a field called Ohio and not with the string literal 'Ohio'.
â Michael Stimson
9 mins ago
add a comment |Â
The problem isn't with the field as specified but with the value, Felix stated it most succinctly, the SQL definitely needs a single quote delimiter for a string literal, as the OP had the query stated the SQL was looking to compare NAME_1 field with a field called Ohio and not with the string literal 'Ohio'.
â Michael Stimson
9 mins ago
The problem isn't with the field as specified but with the value, Felix stated it most succinctly, the SQL definitely needs a single quote delimiter for a string literal, as the OP had the query stated the SQL was looking to compare NAME_1 field with a field called Ohio and not with the string literal 'Ohio'.
â Michael Stimson
9 mins ago
The problem isn't with the field as specified but with the value, Felix stated it most succinctly, the SQL definitely needs a single quote delimiter for a string literal, as the OP had the query stated the SQL was looking to compare NAME_1 field with a field called Ohio and not with the string literal 'Ohio'.
â Michael Stimson
9 mins ago
add a comment |Â
up vote
1
down vote
I wasn't able to replicate your error, but try this:
import arcpy
expression = "NAME_1 = 'Ohio'"
def unique_values(table , field):
with arcpy.da.SearchCursor(table, [field], where_clause=expression) as cursor:
return sorted(row[0] for row in cursor)
uniques = unique_values(r'C:arcgisArcTutorAAAsrcUSA.shp' , 'NAME_2')
for unique in uniques:
print (unique)
The SearchCursor where_clause parameter takes a SQL string as its input. There are more examples here
add a comment |Â
up vote
1
down vote
I wasn't able to replicate your error, but try this:
import arcpy
expression = "NAME_1 = 'Ohio'"
def unique_values(table , field):
with arcpy.da.SearchCursor(table, [field], where_clause=expression) as cursor:
return sorted(row[0] for row in cursor)
uniques = unique_values(r'C:arcgisArcTutorAAAsrcUSA.shp' , 'NAME_2')
for unique in uniques:
print (unique)
The SearchCursor where_clause parameter takes a SQL string as its input. There are more examples here
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I wasn't able to replicate your error, but try this:
import arcpy
expression = "NAME_1 = 'Ohio'"
def unique_values(table , field):
with arcpy.da.SearchCursor(table, [field], where_clause=expression) as cursor:
return sorted(row[0] for row in cursor)
uniques = unique_values(r'C:arcgisArcTutorAAAsrcUSA.shp' , 'NAME_2')
for unique in uniques:
print (unique)
The SearchCursor where_clause parameter takes a SQL string as its input. There are more examples here
I wasn't able to replicate your error, but try this:
import arcpy
expression = "NAME_1 = 'Ohio'"
def unique_values(table , field):
with arcpy.da.SearchCursor(table, [field], where_clause=expression) as cursor:
return sorted(row[0] for row in cursor)
uniques = unique_values(r'C:arcgisArcTutorAAAsrcUSA.shp' , 'NAME_2')
for unique in uniques:
print (unique)
The SearchCursor where_clause parameter takes a SQL string as its input. There are more examples here
answered 20 mins ago
CambrianCatalyst
716
716
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f299020%2fnot-able-to-pass-where-clause-in-searchcursor-in-arcpy%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Skip the bracers, use "NAME_1 = 'Ohio'" (note the use of double and single quotes). If you're only using one field it doesn't need to be a list, use with arcpy.da.SearchCursor(table, field, "NAME_1 = 'Ohio'") as cursor: and you should reduce your problems. What's the next line about? You're making a dictionary but it's not, there's no key:value pairs, surely you want to do sorted([row[0] for row in cursor])?
â Michael Stimson
27 mins ago
@MichaelStimson, OP is using a shapefile. Therefore, the double quotes are necessary.
â Fezterâ¦
23 mins ago
1
@Fezter, no they're not. Try it and see.
â Michael Stimson
22 mins ago
1
This is how it shoul like for shapefile "NAME_1" = 'Ohio'
â FelixIP
21 mins ago
1
I understand your aversion to shapefiles @Fezter, I have found that no quote or bracers are required for any of the common storage types when defining a cursor. It may be that some obscure enterprise database needs them but I've not found one yet that complained.
â Michael Stimson
19 mins ago