arcpy select by attribute doesn't work?

476
9
Jump to solution
02-14-2024 02:32 AM
JohannesBierer
Occasional Contributor III

Hello,

try to select by attributes a feature class, but it doesn't work?

Any ideas why?

 

fields = ["OBJECTID", "TARGET_FID"]
with arcpy.da.SearchCursor(outMerge, fields) as cursor:
    for row in cursor:
        if row[1] > 0:
            
            selClause = str(row[1])
            selClause1 = "OBJECTID = {0} Or TARGET_FID = {0}".format(selClause)
            print (selClause1)
        
            arcpy.management.SelectLayerByAttribute(outMerge, "NEW_SELECTION", selClause1)
            result = arcpy.management.GetCount(outMerge)
            print (result[0])
            if int(result[0]) > 1:
                outIntersLines = "outIntersLines_{}".format(row[0])
                # arcpy.management.PointsToLine(outMerge, outIntersLines)
            else:
                continue
        else: 
            continue

 

 The print statements line 9 looks ok for me: 

OBJECTID = 1 Or TARGET_FID = 1
But there's nothing selected?

 

0 Kudos
1 Solution

Accepted Solutions
RHmapping
New Contributor II

I am assuming outMerge is a path to data? I think you are calling GetCount on the input Feature Class, instead you need to wrap a layer using arcpy.management.MakeFeatureLayer and use that as the first to Select by Attributes, and then GetCount on the layer. Alternatively use getOutput on Select  by Attributes - this exposes a layer created by the tool which will have the count on it. call GetCount on that.

            sel_lyr = arcpy.management.SelectLayerByAttribute(outMerge, "NEW_SELECTION", selClause1).getOutput(0)
            count1 = int(arcpy.management.GetCount(sel_lyr).getOutput(0))

View solution in original post

9 Replies
EdMorris
Esri Contributor

Hi

So if you run a "SQL query" and no rows / features are selected then it means you have a poorly constructed where clause.

I think 'Or' (line 7 in your code snippet above) needs to be capitalised (I think...).

Many thanks ed

 

Amir-Sarrafzadeh-Arasi
Occasional Contributor

Dear EdMorris,

I hope you are doing great when reading this message,

SQL keywords are not case-sensitive, which means you can write them in uppercase, lowercase, or a combination of both.

Have a great day

Amir Sarrafzadeh Arasi
EdMorris
Esri Contributor

thanks!

... as i said... "I think..."

happy to be corrected

RHmapping
New Contributor II

I am assuming outMerge is a path to data? I think you are calling GetCount on the input Feature Class, instead you need to wrap a layer using arcpy.management.MakeFeatureLayer and use that as the first to Select by Attributes, and then GetCount on the layer. Alternatively use getOutput on Select  by Attributes - this exposes a layer created by the tool which will have the count on it. call GetCount on that.

            sel_lyr = arcpy.management.SelectLayerByAttribute(outMerge, "NEW_SELECTION", selClause1).getOutput(0)
            count1 = int(arcpy.management.GetCount(sel_lyr).getOutput(0))
JohannesBierer
Occasional Contributor III

Thank you ...

0 Kudos
Amir-Sarrafzadeh-Arasi
Occasional Contributor

Dear Johannes,

I hope you are doing well,

 

The problem of your script is this, in the for loop you are generating a new SQL clause, but you should add to the previous SQL clause and apply the select method out of for loop.

please check tthe below image, which is correct version of your script.

SQL.png

 

Please make sure about the 'outMerge',

If you have more info please let me know.

 

Hope it helps, Stay Safe

 

Amir Sarrafzadeh Arasi
JohannesBierer
Occasional Contributor III

Hello Amir,

thank you for your help - but i think RHmapping was right about the missing make feature layer ...

0 Kudos
Amir-Sarrafzadeh-Arasi
Occasional Contributor

Hello Johannes,

As I mentioned you should be sure the 'outMerge' is feature layer, but if you have a feature layer with high number of records, calling arcpy inside a for loop in each iteration is not efficient method.

If you try the both solutions in your feature layer data, you will see the differences.

I just tried in a feature layer with 257 records,

first one took 7.94 seconds.

second one took 0.18 seconds.

Anyway, I am so happy you find your solution,

Best ragrads

Amir Sarrafzadeh Arasi
AlfredBaldenweck
MVP Regular Contributor

Looks like you got a solution, but for next time:

If you want to use a selection as input into something else, you need to make that selection a variable.

outMerge = arcpy.management.SelectLayerByAttribute(outMerge, 
                                                   "NEW_SELECTION", 
                                                   selClause1)
result = arcpy.management.GetCount(outMerge,...)