Not sure why this statement wouldn't run within an edit session in arcpy:
edit = arcpy.da.Editor(gdb)
edit.startEditing(False, True)
edit.startOperation()
arcpy.CalculateField_management ('Export_Output', 'Point_Type', 'UNKNOWN')
edit.stopOperation()
edit.stopEditing(True)
I am working with a versioned replica in SQL Express. Working on the default version.
Solution: I just ended up using da.UpdateCursor to calculate all recs to 'UNKNOWN'. This worked fine. Was getting strange results otherwise (on small and large datasets).
Solved! Go to Solution.
Do this for the calculation:
arcpy.CalculateField_management ("swXSectionPoint", "Point_Type", '"UNKNOWN"')
Notice that the expression cannot be "UNKNOWN", it must be '"UNKNOWN"' (single quote, double quote, UNKNOWN, double quote, single quote).
This assumes that swXSectionPoint is a layer recognized by the script and that Point_Type is a field in that layer. There is no bug and support will tell you to write the calculation expression the same as what I have written in this post.
You forgot the quotes for the expression. It needs to be '"UNKNOWN"' Quotes are always the first thing to check whenever you do calculations, especially when they are run in a script.
So single quotes don't work here?
No, that is not the problem. It is that it must quote a quoted string, since the expression in the normal field calculator must be quoted. "UNKNOWN" is what you should enter in the field calculator outside of a script. Inside a script that quoted string needs to be quoted again to send it as a string value with quotes.
So I did use quotes, but just single quotes to quote the string.
In a script you have to quote the expression regardless of what the value inside it contains (field names, numbers and strings all have to be in quotes). So 'UNKNOWN' equates to an unknown value of UNKNOWN (no quotes) to the field calculator, which does not exist and causes an error. You want to sent "UNKNOWN" (a quoted string), so you have to put that in quotes in the script, i.e., ' "UNKNOWN" ' (works for VB Script or Python calculations) or " 'UNKNOWN' " (works only for Python calculations).
Hm, I tried this:
edit = arcpy.da.Editor(gdb)
edit.startEditing(False, True)
edit.startOperation()
# with arcpy.da.UpdateCursor('swXsectionPoint','Point_Type') as unk_csr:
# for unk_rec in unk_csr:
# unk_rec[0]="UNKNOWN"
# unk_csr.updateRow(unk_rec)
arcpy.CalculateField_management ("swXSectionPoint", "Point_Type", "UNKNOWN")
edit.stopOperation()
edit.stopEditing(True)
And it is still giving me strange results. Tried doing it out of an edit session as well and still no luck. Also, the Update Cursor shown commented out above seems to work. Will put in a support request. Perhaps I need to compress the gdb, or maybe I found a bug.
Do this for the calculation:
arcpy.CalculateField_management ("swXSectionPoint", "Point_Type", '"UNKNOWN"')
Notice that the expression cannot be "UNKNOWN", it must be '"UNKNOWN"' (single quote, double quote, UNKNOWN, double quote, single quote).
This assumes that swXSectionPoint is a layer recognized by the script and that Point_Type is a field in that layer. There is no bug and support will tell you to write the calculation expression the same as what I have written in this post.
Ah okay! Thank you!
Ah that did it! Support thought it was a bug too, but they realized after looking further that it was a SQL issue. I have no idea how this has not been an issue for me in the past. I don't remember double-quoting my strings.