Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I want to add a query layer to an arcmap document so that whenever i have a db data update, it is reflected in my ma and I can update my service through python script. This is my script so far...

import arcpy
from arcpy import env
arcpy.env.overWriteOutput = 1

# define local variables
wrkspc = 'C:/ArcGIS_Workspace/'
mxd = arcpy.mapping.MapDocument(wrkspc + 'scripts/Trial.mxd')

input_db_name = "Database Connections/xxx.sde"
outLayer = "DynamicQueryLayer"
out_layer_name = wrkspc +"/shapefiles/DynamicQueryLayer.lyr"
query = "SELECT * FROM TEST_TABLE"

#unique key for the table
oid_field = "schema.TEST_TABLE.TEST_ID"

try:

    # Create a query layer
    arcpy.MakeQueryLayer_management (input_db_name, outLayer, query, oid_field)

    # save the created query layer
    arcpy.SaveToLayerFile_management(outLayer, out_layer_name, "ABSOLUTE")

    # Get the created layer
    lyr = arcpy.mapping.Layer(out_layer_name)

    # get the data frame of the given mxd
    data_frame = arcpy.mapping.ListDataFrames(mxd)[0]

    # Switch to data view
    mxd.activeView = data_frame.name

    arcpy.mapping.AddLayer(data_frame, lyr, 'TOP')
    print "Added layer to the MXD ..."

    mxd.save()
    print "MXD saved..."    
except Exception, e:
    # If an error occurred, print line number and error message
    import traceback, sys
    tb = sys.exc_info()[2]
    print "Line %i" % tb.tb_lineno
    print e.message

I get this error. ERROR 000732: Input Features: Dataset DynamicQueryLayer does not exist or is not supported.

I am sure there is a way to save the created query layer as a layer file.Any help is greatly appreciated.

share|improve this question

1 Answer 1

Use the GP tool or the .save/.saveACopy methods on the Layer object

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.