Select to view content in your preferred language

error in SelectLayerByAttribute

1472
7
Jump to solution
05-05-2012 03:00 PM
ElaineKuo
Regular Contributor
Hello

System: ArcGIS 9.3
pythonWin: 2.5

Problem 1:
Using SelectLayerByAttribute but error showed:
ExecuteError: ERROR 000358: Invalid expression

Please kindly help indicate the mistake in the query code.
Also, a shape file is attached for trial.
Thanks a lot.

 ##Script Name: Delete rows ##Description: delete selected rows ##Created By: Elaine Kuo ##Date: 04/05/2012  #Import standard library modules import arcgisscripting import os  #Create the Geoprocessor object gp = arcgisscripting.create(9.3)  #Set the input workspace #GP.workspace = sys.argv[1] #Set the workspace. gp.Workspace= "G:/temp"  #Set the output workspace #outWorkspace = sys.argv[2] #Set the workspace. List all of the feature classes in the dataset outWorkspace= "G:/temp/test"  #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses()  # Loop through every item in the list that was just generated for fc in fcs:      # Break out the name, no path or extension, using the describe object.     desc = gp.describe(fc)     featureName = desc.name      # Make featureclasses as layers     gp.OverWriteOutput = 1     gp.toolbox = "Data Management"     gp.MakeFeatureLayer("geor0313_p.shp","geor0313_p_lyr")          #Get a list of the fields in the featureclass     fields = gp.ListFields("geor0313_p_lyr", "C*", "String")      # Loop through every item in the list that was just generated      for field in fields:          gp.toolbox = "Data Management"               # Select records to be deleted (C*, i.e. C7658)         query = "\"C*\" = 'R'"         gp.SelectLayerByAttribute("geor0313_p_lyr", "ADD_TO_SELECTION", query)          # Delete selected records         gp.deleterows("geor0313_p_lyr")              #Validate the new feature class name for the output workspace.     outFeatureClass = outWorkspace + os.sep +GP.ValidateTableName("geor0313_p_lyr",outWorkspace)       
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
FabianBlau
Deactivated User
In arcgisscripting 9.3 ListFeatureClasses() returns not a string but an object.
http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=ListFields_method.

This object is like a container for severals varaibles and functions (for a  detailed explanation of objects read some smart books;) http://en.wikipedia.org/wiki/Object_%28computer_science%29)
One of the attributes is the fieldname -> http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Field_properties.


Use AddMessage when you start a script from a toolbox.
Use print starting a script in python.
print type(field)

View solution in original post

0 Kudos
7 Replies
BruceNielsen
Frequent Contributor
The where clause in selectlayerbyattribute does not recognize wildcard characters. You need to use the field variable that you're creating in the inner loop instead. Try this:
query = "\"%s\" = 'R'" % field

This will insert the field variable into the query expression each time the loop executes.
0 Kudos
ElaineKuo
Regular Contributor
Hello,

system arcGIS 9.3

Thanks for the response.
However, the same error still occurred after modifying the code as below.
Please kindly help and thanks.

##Script Name: Delete rows
##Description: delete selected rows
##Created By: Elaine Kuo
##Date: 04/05/2012

#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

#Set the input workspace
#GP.workspace = sys.argv[1]
#Set the workspace.
gp.Workspace= "G:/temp"

#Set the output workspace
#outWorkspace = sys.argv[2]
#Set the workspace. List all of the feature classes in the dataset
outWorkspace= "G:/temp/test"

#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()

# Loop through every item in the list that was just generated
for fc in fcs:

    # Break out the name, no path or extension, using the describe object.
    desc = gp.describe(fc)
    featureName = desc.name

    # Make featureclasses as layers
    gp.OverWriteOutput = 1
    gp.toolbox = "Data Management"
    gp.MakeFeatureLayer("geor0313_p.shp","geor0313_p_lyr")
    
    #Get a list of the fields in the featureclass
    fields = gp.ListFields("geor0313_p_lyr", "C*", "String")

    # Loop through every item in the list that was just generated 
    for field in fields:

        gp.toolbox = "Data Management"
     
        # Select records to be deleted (C*, i.e. C7658)
        query = "\"%s\" = 'R'" % field
        gp.SelectLayerByAttribute("geor0313_p_lyr", "ADD_TO_SELECTION", query)

        # Delete selected records
        gp.deleterows("geor0313_p_lyr")
        
    #Validate the new feature class name for the output workspace.
    outFeatureClass = outWorkspace + os.sep +GP.ValidateTableName("geor0313_p_lyr",outWorkspace)


    
0 Kudos
FabianBlau
Deactivated User
I guess field is a Field Object, not a String. Check this with gp.addMessage(type(field)).
So try:
query = "\"%s\" = 'R'" % field.Name
0 Kudos
ElaineKuo
Regular Contributor
Thanks a lot.
You are right.
Please kindly explain why it works with "query = "\"%s\" = 'R'" % field.Name" instead of "query = "\"%s\" = 'R'" % field"
Also, please kindly help how to run gp.addMessage(type(field)) using PythonWin for a beginner. 🙂
0 Kudos
FabianBlau
Deactivated User
In arcgisscripting 9.3 ListFeatureClasses() returns not a string but an object.
http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=ListFields_method.

This object is like a container for severals varaibles and functions (for a  detailed explanation of objects read some smart books;) http://en.wikipedia.org/wiki/Object_%28computer_science%29)
One of the attributes is the fieldname -> http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Field_properties.


Use AddMessage when you start a script from a toolbox.
Use print starting a script in python.
print type(field)
0 Kudos
ElaineKuo
Regular Contributor
Thanks a lot.

How to mark that you helped answer my questions 😮
0 Kudos
FabianBlau
Deactivated User
There is a button on the right side (sign in first) :D.
0 Kudos