Select to view content in your preferred language

SelectByAttribute ERROR 000358: Invalid expression

5023
5
04-03-2010 03:13 AM
GalAvraham
Emerging Contributor
Hi All
Bellow is a python script originated from a model and modified by me for certain needs.
This code is applied to data elements (i.e. tables /feature classes) stored within perssonal SDE environment, built on top of SQL Server Express (2008). The ArcGIS version is 9.3.1, with the default python installation.

When executing the script I keep getting the following error:

<class 'arcgisscripting.ExecuteError'>: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).

Here is the relevant part of the code that might cause that problem:


tblrows = gp.searchcursor (Table_View)
tblrow = tblrows.next()

while tblrows:
    myDate = tblrow.getvalue ("Date")
    LineNo = tblrow.getvalue ("route_No")
    FromStation = tblrow.getvalue ("From_place")
    ToStation = tblrow.getvalue ("To_place")
    Status = 1 #tblrow.getvalue ("Status")

    phrase1 = "\"Line_No\" =" + str(LineNo) + " AND \"Station_No\" > " + str(FromStation)
    phrase2 = "\"To_Station\" <=" + str(ToStation)

    # Process: = Line_No and > FromStation...
    gp.SelectLayerByAttribute_management(Demining_Layer, "NEW_SELECTION", phrase1)

    # Process: <= ToStation...
    gp.SelectLayerByAttribute_management(Flags_Layer, "SUBSET_SELECTION", phrase2)

    # Process: Update Visit Date...
    gp.CalculateField_management(Station_to_be_Updated, "Visit_Date", myDate, "VB", "")

    # Process: Update Status...
    gp.CalculateField_management(Flags_Layer__3_, "Status", str(Status), "VB", "")

    myDate = ""
    LineNo = ""
    FromStation = ""
    ToStation = ""
    Status = ""

    tblrow = tblrows.next ()


del tblrow
del tblrows



I guess it's got something to do with the way I'm transfering the sql phrase to the  SelectByAttribute tools, but I can't put my finger at the exact spot.

Any help will be appreciated

Thanks in advance

Gal
0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus
should this line
while tblrows
not be
while tblrow
0 Kudos
DanPatterson_Retired
MVP Emeritus
just curious... was that it? or are there further errors? or does the forum not allow for threads to be finished?
0 Kudos
MikeHouts
Emerging Contributor
I built a model in model builder that selected polygons by attributes, and had it set up where the selection criteria was entered as user defined parameters...worked fine.  I exported the model to a python script and initially it worked fine, but the interactive parameters were not active, just default values from when I built it, but it worked.  I edited the script properties to add parameters and now it wont run.  It keeps giving me the error shown below. 

And what is with the "Running script test..." and "failed to execute (test)"  no part of the name is test


Running script test...
<class 'arcgisscripting.ExecuteError'>: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).
Failed to execute (test).

Any help would be greatly appreciated
0 Kudos
MarkZito
Occasional Contributor
If you are selecting data from a string field or any non number field than you need to add single quotes around the value being selected.  Such as..
phrase1 = "\"Line_No\" =" + "'"+str(LineNo)+"'" + " AND \"Station_No\" > " + str(FromStation)
0 Kudos
ChrisSnyder
Honored Contributor
Try changing:

phrase1 = "\"Line_No\" =" + str(LineNo) + " AND \"Station_No\" > " + str(FromStation)
phrase2 = "\"To_Station\" <=" + str(ToStation

to this:

phrase1 = "Line_No = " + str(LineNo) + " AND Station_No > " + str(FromStation)
phrase2 = "To_Station <= " + str(ToStation)

Thhat is, get rid of all the \s
0 Kudos