insert cursor arcgis online via python

2308
5
08-22-2018 01:51 PM
by Anonymous User
Not applicable

Hey there, I don't know if this is the right space to ask this question but I figure it is worth a shot. I am trying to use an insert Cursor in a script to update a table hosted in ArcGIS Online. My script has no problem updating tables in databases however I cannot for the life of me figure out how to edit the feature service.

also you should know I barely know what I am doing. I can connect to my account via python but that's about it. I assume this is pretty simple so hopefully one of you folks can guide me!

thanks,

  Dave

Tags (2)
5 Replies
JoeBorgione
MVP Emeritus

I believe you need to use the ArcGIS API for Python when working on Hosted data:  ArcGIS API for Python | ArcGIS for Developers 

That should just about do it....
DougBrowning
MVP Esteemed Contributor

See my post here on search cursor - not sure on insert.  It does work for all but tables.  As you can see esri acknowledged the error but did not respond what to do about it.  I bugged a few times but just had to give up on it.

https://community.esri.com/thread/213089-using-a-agol-hfs-in-python-directly

0 Kudos
RandyBurton
MVP Alum

You can also use the Rest API. See:  Add Features and Update Features .

There are forms to add and update features using the REST API.  Sign in to your account.  In the content section select the feature layer you want, then (on next page) select the specific layer.  This will take you into the services directory and show you basic info about your layer.  At the bottom of this page, under supported operations, there is a link to the Add Features and Update Features forms.  I would suggest learning with a practice/backup feature until you are comfortable with the process.

RandyBurton
MVP Alum

A sample script using the REST API:

import urllib, urllib2, json, sys, time, arcpy

# Credentials and feature service information
username = <username>
password = <password>

# addList (geometry and fields) : [ X_LON, Y_LAT, Name, C_long, F_date ]
addList = [
    [ -8572606.3109, 4705898.3536000028, 'US Capitol', 20180822, '2018-08-22 12:00:00' ]
    ]

URL = "https://<server info>/arcgis/rest/services/example/FeatureServer/0/addFeatures"

# obtain a token
referer = "http://www.arcgis.com/"
query_dict = { 'username': username, 'password': password, 'referer': referer }
query_string = urllib.urlencode(query_dict)
url = "https://www.arcgis.com/sharing/rest/generateToken"
token = json.loads(urllib.urlopen(url + "?f=json", query_string).read())

if "token" not in token:
    print(token['error'])
    sys.exit(1) # exit if no token

for item in addList :

    f_date = int(time.mktime(time.strptime(item[4], "%Y-%m-%d %H:%M:%S")))*1000 # UTC in miliseconds
    
    feature_data = [{
        "attributes": { "Name": item[2], "C_long": item[3], "F_date": f_date },
        "geometry": { "x": item[0], "y": item[1] }}]
    # print feature_data
    
    submitData = {
        "features": json.dumps(feature_data),
        "f": "json",
        "token": token['token']
        }
    # print submitData

    jsonResponse = urllib.urlopen(URL, urllib.urlencode(submitData))
    jsonOutput = json.loads(jsonResponse.read())

    print json.dumps(jsonOutput, indent=4, sort_keys=False)
    # parse jsonOutput for results
by Anonymous User
Not applicable

thanks for this! because of you I will look like a GIS all star.

0 Kudos