Mulit query issue

693
6
08-20-2021 08:55 AM
CCWeedcontrol
Occasional Contributor III

I have the following stand alone script but it deletes all the features in the feature class instead what I have in the query. It also only prints out that there is 9 records that meet the query criteria witch is not correct there are more than 9 records. Seems pretty straight forward but I am Not sure what I am doing wrong.

I also tried using updatecursor but I get the same results, it deletes are the features in the feature class.

TaxPar = r"C:\Temp\TaxParcels1.shp"

# Process: Make Feature Layer
query = "DXF_TEXT LIKE 'Q%' or DXF_TEXT = ' ' or DXF_TEXT IS NULL"
arcpy.SelectLayerByAttribute_management(TaxPar, "NEW_SELECTION", query)
result = arcpy.GetCount_management(TaxPar).getOutput(0)
print ('{} has {} records'.format(TaxPar, result[0]))

if int(arcpy.GetCount_management(TaxPar).getOutput(0)) > 0:
    arcpy.DeleteFeatures_management(TaxPar)

 

 

sql = "DXF_TEXT LIKE 'Q%' or DXF_TEXT = '' or DXF_TEXT IS NULL"
with arcpy.da.UpdateCursor(TaxPar, 'DXF_TEXT',sql) as cursor:
    for row in cursor:
       cursor.deleteRow()
    else:
        pass
0 Kudos
6 Replies
JoeBorgione
MVP Emeritus

I don't see where you actually make a feature layer after line 3 in the first code block.

 

Edited moments later: are you really using a shape file, because they don't support <Null> values....

That should just about do it....
CCWeedcontrol
Occasional Contributor III

Sorry about that, I had different variations trying to figure out what the problem is. Even when adding arcpy.MakeFeatureLayer_management it still deletes all the features.

 

 

TaxPar = r"C:\Temp\TaxParcels1.shp"

# Process: Make Feature Layer
arcpy.MakeFeatureLayer_management(TaxPar, "Par")
query = "DXF_TEXT LIKE 'Q%' or DXF_TEXT = ' ' or DXF_TEXT IS NULL"
arcpy.SelectLayerByAttribute_management("Par", "NEW_SELECTION", query)
result = arcpy.GetCount_management("Par").getOutput(0)
print ('{} has {} records'.format("Par", result[0]))

if int(arcpy.GetCount_management("Par").getOutput(0)) > 0:
    arcpy.DeleteFeatures_management("Par")

 

 

 

0 Kudos
JoeBorgione
MVP Emeritus

I created a shapefile just to try what you are doing I get really weird results in a SQL select window.  I bet I haven't created a shapefile in over 10 years, and this is one of the reasons why:

JoeBorgione_1-1629753781905.png

 

JoeBorgione_0-1629753677171.png

 

JoeBorgione_2-1629753849523.png

 

 

That should just about do it....
0 Kudos
CCWeedcontrol
Occasional Contributor III

The file is a in filegeodata base, I exported out to a shape to test.

I restarted my pc and low and behold it worked.

0 Kudos
JoeBorgione
MVP Emeritus

If your data is in a FGDB why bother testing on a shapefile?

That should just about do it....
0 Kudos
CCWeedcontrol
Occasional Contributor III

Because it wasn't working in a FGDB.

0 Kudos