Python API field calculator

998
3
12-16-2019 04:09 PM
MaazaMekuria
Occasional Contributor

I have been inclined to use the layer calculator since it is much faster than updating features using the API.  The feature update also fails if for example I try to update a large set of features at one time.  The layer calculator does the job much faster but I am having to scale the query as it limits the type of query.

here is what I have encountered a problem with.  I want to limit the calculation query by a date field and a time stamp or any way that makes comparing a field and a date possible.  How can I coerce the date data or the field to match a certain format? 

  Below is a code snippet

val = datetime.today()  # today's date

fld = 'todayis'  # featrue table field containng datetime

valx = datetime.strftime(datetime.today(),'%Y%m%d')

qry0 = "format({},'yyyyMMdd') <> '{}'".format(fld,val)

val2 = datetime.today() + timedelta(days=7)

fld2="nextweekdy'

result = lyr.calculate(where=qry0,calc_expression={"field" : fld2 , "value" : val2})

Running this code I get an error saying where parameter is invalid.  

RuntimeError:
'where' parameter is invalid
(Error Code: 400)

any insights?

0 Kudos
3 Replies
KarstenRank
Occasional Contributor III

Hi,

I think something is missing in your where query:

lyr.calculate(where="fld==qry0",calc_expression={"field" : fld2 , "value" : val2})‍‍
JoshuaBixby
MVP Esteemed Contributor

A WHERE clause of "fld==qry0" is not correct for a couple of reasons.  First, the SQL equality operator is a single equal sign, =, and not a double, ==.  Second, the variable qry0 is a Python construct, and neither SQL nor the server know anything about it so it will generate an error.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor