identifying the type of database

2655
4
Jump to solution
06-02-2011 11:34 AM
BrianMerson
New Contributor III
I'm new to both Python and arcpy.  I couldn't find the answer to this in the docs or in searching the forums.

I have a script that gets an input feature class.  I need to make a query on the class.  However, personal geodatabases use a different query string field name format ([FIELD]) than do all the file-based databases ("FIELD").  I could make the query string a parameter and force the user to deal with this, but for reasons too boring to go into, I'd prefer to build the query myself.

Is there some way to find out what type of database you are querying given a feature class from that database?   I saw that the Describe function for workspaces provides some information, but it does not seem to distinguish between database types.  So far, I have been unable to find a way to do this, but that could easily be because I don't know precisely where to look yet.  Any help would be appreciated.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
CoryMacNeil1
Occasional Contributor II
Hi,

You could try using arcpy.Describe on your feature class and then use the path of that feature class.  Within the path you could look for .gdb (file geodatabase) or .mdb (personal geodatabase) and then use If...Else statements to deal with the different feature classes.

desc = arcpy.Describe ("FeatureClass")
print desc.path

output- C:\data\FileGeodatabase.gdb

I hope this helps.
Cory

View solution in original post

4 Replies
CoryMacNeil1
Occasional Contributor II
Hi,

You could try using arcpy.Describe on your feature class and then use the path of that feature class.  Within the path you could look for .gdb (file geodatabase) or .mdb (personal geodatabase) and then use If...Else statements to deal with the different feature classes.

desc = arcpy.Describe ("FeatureClass")
print desc.path

output- C:\data\FileGeodatabase.gdb

I hope this helps.
Cory
BrianMerson
New Contributor III
Of course.  That makes perfect sense.  For some reason I was just having a mental block on the idea of looking at the paths.  Thanks.
0 Kudos
ChrisFox3
Occasional Contributor III
Also I would add that once you have the path to the feature class you can use the function 'AddFieldDelimeters' to add the proper field delimeters for the field based on the workspace type. It will automatically know how to delimit a field in a file GDB vs. a PGDB for example.

fc = arcpy.getParamaterAsText(0)
fieldName = arcpy.getParamaterAsText(1)
newName = arcpy.AddFieldDelimiters(fc, fieldName)
sqlExp = newName + " = 5"
RDHarles
Occasional Contributor
Just use the "workspace type" option when you list your workspaces:

import arcpy, os

arcpy.env.workspace = os.getcwd()

for gdb in arcpy.ListWorkspaces("*","Access"):
    print "Access: "+gdb

for gdb in arcpy.ListWorkspaces("*","FileGDB"):
    print "FGDB: "+gdb    
0 Kudos