arcpy SelectByAttribute_management

124
2
04-30-2020 08:46 AM
FakherRouatbi
New Contributor

hi, i try to add a point xy to feature class and i want to select the latest record in table (recently added). The script runs and I don't have any error message but the tool doesn't select any thing. How can I fix this?


import arcpy
arcpy.env.workspace = r'D:\Geodatabases\BD_VOCATION.gdb '
points = r'D:\Geodatabases\BD_VOCATION.gdb\Loc_vocation'
try:
arcpy.MakeFeatureLayer_management(points,'points_layer')
rows = arcpy.SearchCursor(points,'','',"OBJECTID", "OBJECTID D")
lst = [] for row in rows:
m = row.getValue("OBJECTID")
lst.append(m)
v = lst[0]
r= str(v)
query = """{0} = {1}""".format(arcpy.AddFieldDelimiters(points, "OBJECTID"), int(r))
arcpy.SelectLayerByAttribute_management("points_layer", "NEW_SELECTION", query)
except:
print
"An error occured during selection"

0 Kudos
2 Replies
JoeBorgione
MVP Esteemed Contributor

perhaps you could paste your code into a python syntax highlighter window so we can see it easier....

can't wait to retire....
JoeBorgione
MVP Esteemed Contributor
import arcpy
arcpy.env.workspace = r'D:\Geodatabases\BD_VOCATION.gdb '
points = r'D:\Geodatabases\BD_VOCATION.gdb\Loc_vocation'
try:
arcpy.MakeFeatureLayer_management(points,'points_layer')
rows = arcpy.SearchCursor(points,'','',"OBJECTID", "OBJECTID D")
lst = []
for row in rows:
m = row.getValue("OBJECTID")
lst.append(m)
v = lst[0]
r = str(v)
query = """{0} = {1}""".format(arcpy.AddFieldDelimiters(points, "OBJECTID"), int(r))
arcpy.SelectLayerByAttribute_management("points_layer", "NEW_SELECTION", query)
except:
print "An error occured during selection"
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This is what I meant for formatting.  I indented the best I could but since I don't understand your logic Lines 13 and 14 may not be part of the for loop.  However, that said, a few things jump out:

You should be using da.SearchCursor; I don't know if the original SearchCursor works any more.

You are mixing and matching between your feature class and your feature layer.

The syntax in line 6 even for the old search cursor isn't correct. I suggest you use the more 'traditional' syntax to a search cursor as shown in the help pages.Can you explain your logic in lines 9 through 12? 

Finally, what is your end game?  That is, what are you trying to accomplish?

can't wait to retire....
0 Kudos