I have created an SDE database in which I have copied feature classes from my production database from State Plane into WGS 84 for web mapping applications. The final step is to create views, and I am trying to automate the process as I have hundreds of feature classes.
Below, I am trying to make a list of all the feature classes and use the name of the feature class. The middle parameter starts with a prefix of v_ then grabs the feature class name (ie: v_watermeters). The third variable is the SQL statement used to create the view definition, where it grabs all the fields (SELECT * FROM ) then I want to use the feature class name (ie: SELECT * FROM watermeters).
I am having trouble with the third variable and concatenating the string values to make valid code.
We are currently on ArcGIS 10.3.1 and SQL Server 2012.
import arcpy
import os
arcpy.env.workspace = "Database Connections\\Server Database sde.sde"
featureclasses = arcpy.ListFeatureClasses()
for fc in featureclasses:
arcpy.arcpy.CreateDatabaseView_management(
fc, os.path.join("Database Connections\\Server Database sde.sde","v_"+str(featureclasses), "select * from "+featureclasses)
Solved! Go to Solution.
Hi,
Try following code :
import arcpy
import os
arcpy.env.workspace = "Database Connections\\Server Database sde.sde"
featureclasses = arcpy.ListFeatureClasses()
for fc in featureclasses:
arcpy.arcpy.CreateDatabaseView_management(
"Database Connections\\Server Database sde.sde", "v_" + str(fc.split('.')[2]), "select * from " + fc)
Consider the following, and note that the individual item is 'fc' and the list, which never changes, is 'featureclasses':
featureclasses = ['a', 'b', 'c']
for fc in featureclasses:
print('start')
print(fc, featureclasses)
print('end')
start
a ['a', 'b', 'c']
end
start
b ['a', 'b', 'c']
end
start
c ['a', 'b', 'c']
end
In line 8, you refer twice to 'featureclasses' which is the name of the list you are iterating through. I'm not sure why you are doing that. It seems to me you would want the value of fc instead, the current item of the list.
Personally, I prefer to use the format() function when I can to concatenate characters and variable values.
for fc in featureclasses:
arcpy.arcpy.CreateDatabaseView_management(
fc, os.path.join("Database Connections\\Server Database sde.sde",'v_{}'.format(fc), 'select * from {}'.format(fc)
I updated line 6 and 7 with your code but seems to still report an error:
Traceback (most recent call last):
File "C:\path\to\python\file.py", line 8, in <module>
arcpy.arcpy.CreateDatabaseView_management(fc, os.path.join("Database Connections\\Server DB sde.sde",'v_{}'.format(fc), 'select * from {}'.format(fc)))
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 4069, in CreateDatabaseView
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000837: The workspace is not the correct workspace type.
ERROR 000735: View Definition: Value is required
Failed to execute (CreateDatabaseView).
import arcpy
import os
arcpy.env.workspace = "Database Connections\\Server DB sde.sde"
featureclasses = arcpy.ListFeatureClasses()
for fc in featureclasses:
arcpy.arcpy.CreateDatabaseView_management(fc, os.path.join("Database Connections\\Server DB sde.sde",'v_{}'.format(fc), 'select * from {}'.format(fc)))
Check your parameters against the help.
Create Database View—Help | ArcGIS for Desktop
The tool takes 3 parameters.
1.) database
2.) view name
3.) view definition
Also, am I seeing double or do I see "arcpy.arcpy"? That is a red flag.
# ---- what you have
featureclasses = ['a', 'b', 'c']
'v_' + str(featureclasses)
"v_['a', 'b', 'c']"
# ---- what you want, one at a time
fc = featureclasses[0]
'v_' + str(fc)
'v_a'
I'm not familiar with the CreateDataBaseView_management() function, but error 837 catches my attention. Again, just my personal preference, but I would create a varable to hold the database name parameter and insert it rather than using the os.path.join() method where you do. I just like laying things out...
Hi,
Try following code :
import arcpy
import os
arcpy.env.workspace = "Database Connections\\Server Database sde.sde"
featureclasses = arcpy.ListFeatureClasses()
for fc in featureclasses:
arcpy.arcpy.CreateDatabaseView_management(
"Database Connections\\Server Database sde.sde", "v_" + str(fc.split('.')[2]), "select * from " + fc)
I'm not the guy looking for a solution, but I'm sure he'll give it a spin....
I think fc.split('.')[-1]
will work with more platforms.