Select to view content in your preferred language

UpdateCursor and aliases

775
2
10-18-2018 10:02 AM
CynthiaKozma
Frequent Contributor

I am writing a Python script that will run automatically every night during the election period.  It will take a .csv file from a folder and updates the data in a precinct feature layer that is read in an Operations Dashboard.  Right now, the precinct layer is built with Name and an Alias.  The Alias spells out the candidate/contest name with spaces.  When I run my python script, it bails at.......for uRow in uCursor:  uCursor.updateRow(row)......with an 'invalid sql statement' error.  I went in an removed all spaces from the alias, and the script runs fine.  Do I need to go back and change the alias in all the fields or is there a workaround to get the UpdateCursor to run?  The alias works well with Operations Dashboard in that it then labels the bar chart with the correct name automatically.  Any ideas?

Tags (2)
0 Kudos
2 Replies
JoshuaBixby
MVP Esteemed Contributor

It helps to provide some actual code, especially how you are setting up the cursor and the updated row.

CynthiaKozma
Frequent Contributor
polyCount = int(arcpy.GetCount_management(precincts)[0])
counter = 1
seqFld = "SEQUENCE"
queryString = '"' + seqFld + '" = {}'.format(counter)
print str(counter) + " of " + str(polyCount)
arcpy.SelectLayerByAttribute_management("precincts", "NEW_SELECTION", queryString)
for i in range(1, 178):
    queryString = '"' + seqFld + '" = {}'.format(counter)
    arcpy.SelectLayerByAttribute_management("precincts", "NEW_SELECTION", queryString)
    precValue = ([r[0] for r in arcpy.da.SearchCursor("precincts", ["CODE"])])
    precNum = precValue[0]
    print precNum
    queryPrec = "Code = " + str(precNum)
    fields = ['Reg_voters', 'Turn_out', 'No19Rep']
    arcpy.SelectLayerByAttribute_management("csvGdbase", "NEW_SELECTION", queryPrec)
    arcpy.SelectLayerByAttribute_management("csvGdbase", "SUBSET_SELECTION", queryCont1)
    arcpy.SelectLayerByAttribute_management("csvGdbase", "SUBSET_SELECTION", queryCand1)
    with arcpy.da.SearchCursor("csvGdbase", ("Reg_voters", "Turn_Out", "total_votes")) as cursor:
        for row in cursor:
            print row[0]
            print row[1]
            print row[2]
            with arcpy.da.UpdateCursor(precincts, fields) as uCursor:
                for uRow in uCursor:
                    uCursor.updateRow(row)
   
    arcpy.SelectLayerByAttribute_management("csvGdbase", "NEW_SELECTION", queryPrec)
    arcpy.SelectLayerByAttribute_management("csvGdbase", "SUBSET_SELECTION", queryCont1)
    arcpy.SelectLayerByAttribute_management("csvGdbase", "SUBSET_SELECTION", queryCand2)
    with arcpy.da.SearchCursor("csvGdbase", ("total_votes")) as cursor2:
        for row1 in cursor2:
            print row1[0]
            with arcpy.da.UpdateCursor(precincts, ['No19Main']) as uCursor1:
                for uRow1 in uCursor1:
                    uCursor1.updateRow(row1)

Hopefully, this is enough of the code.  The geodatabase 'precincts' is already set up with fields - names and alias.  It just needs to be populated with updated information from a csv file.  If the field 'No19Rep' has an alias of 'No 19 Repealed', then the script bails out.  I changed just this field to debug and found that it runs without any spaces in the alias.  Would love to keep the spaces, but if there isn't a way around it, then I will go through and change all of them.

0 Kudos