Not Able to Pass Where Clause in SearchCursor in ArcPy

The name of the pictureThe name of the pictureThe name of the pictureClash 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:









share|improve this question























  • 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














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:









share|improve this question























  • 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












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:









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 54 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
















  • 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










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:
...





share|improve this answer






















  • 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

















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






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "79"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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:
    ...





    share|improve this answer






















    • 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














    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:
    ...





    share|improve this answer






















    • 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












    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:
    ...





    share|improve this answer














    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:
    ...






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 34 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
















    • 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












    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






    share|improve this answer
























      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






      share|improve this answer






















        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






        share|improve this answer












        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 20 mins ago









        CambrianCatalyst

        716




        716



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?