What are displayed in the tableName are polygon features in a geodatabase. I would like for each row in a loop to take each row TableName and apply the sql query from the sqlstring field and make the feature according to that query that corresponds.

    qname   TheTableName SqlString
0   shp_q1  AST_A        SELECT ...
1   shp_q2  AST_B        SELECT ...
2   shp_q3  AST_PX       SELECT ...

I want to make a new feature polygon according to what the query describes, named as the qname column is named in that row.

Example: Take the AST_A feature polygon, apply the query that it has in the SqlString column and make a new feature polygon based on that query called shp_q1

Here I access the columns that are needed:

datasetList = arcpy.ListTables("*")
for dataset in datasetList:
    with arcpy.da.SearchCursor(dataset, ["TheTableName","SqlString"]) as cur:
        for row in cur:
            query = row[1] #to access the query part of the row
                arcpy.SelectLayerByAttribute_management(row[0], "NEW_SELECTION", query)

row[0] is the 'TheTableName' column and row[1] is the SqlString column.

How should I change the SelectLayerByAttribute_management function parameters to achieve that?

  • You want to output new feature classes based on your condition or what is the step after the selection? – BERA Oct 22 '18 at 10:56
  • Yes, new outputs that will show what the query describes that will be named like the qname column. – user130347 Oct 22 '18 at 10:58

Use Select_analysis, it will output a feature class based on a Query:

import arcpy, os

table_workspace = r'C:\someFolderOrDatabase'
polygon_feature_database = r'C:\someDataBase.gdb'
shapefile_output_folder = r'C:\someFolder'

arcpy.env.workspace = table_workspace
tableList = [os.path.join(table_workspace, table) for table in arcpy.ListTables("*")]

arcpy.env.workspace = polygon_feature_database
for table in tableList:
    with arcpy.da.SearchCursor(table, ["qname","TheTableName","SqlString"]) as cur:
        for row in cur:
            query = row[2]
            arcpy.Select_analysis(in_features=row[1], out_feature_class=os.path.join(shapefile_output_folder,row[0]+'.shp'),where_clause=query)

If you want to save output to a file geodatabase:

shapefile_output_folder = r'C:\someOutputDatabase.gdb'
... out_feature_class=os.path.join(shapefile_output_folder,row[0])...
| improve this answer | |
  • 1
    I didn't know about select analysis.Very creative approach. thanks. I will study it for a moment. – user130347 Oct 22 '18 at 11:18
  • Can you please add also an alternative way to export the results in the geodatabase itself and not folder. – user130347 Oct 22 '18 at 11:24
  • Just change shapefile_output_folder = r'C:\someFolder' to point at a database and remove +'.shp' – BERA Oct 22 '18 at 11:26
  • And at the last line put this: out_feature_class=os.path.join(gdb_path,row[0]? – user130347 Oct 22 '18 at 12:42
  • out_feature_class=os.path.join(shapefile_output_folder,row[0]) – BERA Oct 22 '18 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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