[GIS] Checking if SQL where clause is valid with Make Feature Layer

arcpysql

Unfortunately it's possible to apply an invalid SQL where clause when using arcpy.MakeFeatureLayer_management. This can cause issues later in a script. For example, I created a feature layer and subsequently tried to apply Snap_edit to the layer, which caused IDLE to crash without throwing an error. The cause was a misspelled field name.

Is there an efficient way to check if an SQL was applied properly to a feature layer?

Best Answer

Proper Python error checking using try catch should at least allow you to prevent a crash.

Apart from that, ArcPy offers the ValidateTableName and ValidateFieldName methods to do basic checks that can be used in code to possibly catch such errors, but note these tools DO NOT check for existing tables and fields, but allow you to test if a new supplied table or field name is valid in the context of the given geodatabase environment.

See this ArcGIS Help topic: Validating table and field names in Python

In case of the ValidateTableName method, you can see it actually modifies a supplied name to be valid, so in order to catch a mispelled table name, you would need to test the output of the method against the supplied table name. Again: this option doesn't check for existing tables and field names!

In order to test for the true presence of existing tables and fields, you could use the

arcpy.ListFeatureClasses

arcpy.ListTables

and

arcpy.ListFields

to do basic checks for validity.