how do I read a table that is in a geodatabase

5183
11
04-05-2011 03:30 PM
TerrySilveus
Occasional Contributor III
how do I read a table that is in a geodatabase

I tried putting this into a arcpy.SearchCursor but its not working (not a feature class or shapefile).  I can't read it with open because it's not a file in that sense.  So how do you load a table located in a geodatabase using python code and iterate through the rows to read some value from a specific column?
Tags (2)
0 Kudos
11 Replies
ChrisMathers
Occasional Contributor III
Can you post the code you are trying? A search cursor should be able to open a table in a GDB.
0 Kudos
TerrySilveus
Occasional Contributor III
This is just for my benefit... I'm trying to teach myself python.  I have programming experience with other languages, but not python and not arcpy (or arcgisscripting for that matter)

import arcpy

#Initialize variables
searchDSParam = None
searchLinkParam = None
searchFieldParam = None
updateDSParam = None
updateLinkParam = None
updateFieldParam = None

#arcGIS toolbox script
#searchDSParam = arcpy.GetParameterAsText(0)
#searchLinkParam = arcpy.GetParameterAsText(1)
#searchFieldParam = arcpy.GetParameterAsText(2)
#updateDSParam = arcpy.GetParameterAsText(3)
#updateLinkParam = arcpy.GetParameterAsText(4)
#updateFieldParam = arcpy.GetParameterAsText(5)

#for testing
searchDSParam = "C:\GIS\VirtualCampus\SpatialStatsModelBuilder\ModelBuilderTutorial\CDCWonderAnalysis.gdb\HeartDiseaseSouth_1999"
searchLinkParam = "CountyCode"
searchFieldParam = "HD_Rate"
updateDSParam = "C:\GIS\VirtualCampus\SpatialStatsModelBuilder\ModelBuilderTutorial\CDCWonderAnalysis.gdb\HD_Rate_1999"
updateLinkParam = "FIPS"
updateFieldParam = "HDrate"

updateTheRow = None
updateTheRows = None
searchTheRow = None
searchTheRows = None
updateTheFields = None
searchTheField = None
searchTheFields = None
fldType = None
fldPrecision = None
fldScale = None
fldLength = None
whereClause = None
linkTypeText = False

#Dictionary to convert return value from field.type to set value using AddField
fieldTypeDictionary = {'SmallInteger': 'SHORT', 'Integer': 'LONG', 'Single': 'FLOAT', 'Double': 'DOUBLE', 'String': 'TEXT', 'Date': 'DATE'}
changed = 0
try:
    #get work done
    #add field to update dataset if necessary
    #search cursor has data to be applied to update cursor (in searchFieldParam field)
    #update cursor has field to be updated (updateFieldParam gets data from searchFieldParam)
    #link update cursor with search cursor on fields searchLinkParam/updateLinkParam

    updateTheFields = arcpy.ListFields(updateDSParam) #get a list of fields in shapefile to be updated
    searchTheFields = arcpy.ListFields(searchDSParam) #get a list of fields in the shapefile to be searched
    for searchTheField in searchTheFields:
        if searchTheField.name == searchFieldParam:
            fldType = searchTheField.type
            fldPrecision = searchTheField.precision
            fldScale = searchTheField.scale
            fldLength = searchTheField.length
        if searchTheField.name == searchLinkParam:
            if searchTheField.type == "String":
                linkTypeText = True
            
    #check to see if it has the field that will be updated, if it has it good; if not then add the field
    #NOT SURE THIS IS WORKING FOR THE CASE WHEN FIELD ALREADY EXISTS - HAVE NOT TESTED YET - BUT IT ADDS FIELD OK (Have been deleting the field for testing)
    if not updateFieldParam in updateTheFields:
        #field does not exist - need to add field
       arcpy.AddField_management(updateDSParam,updateFieldParam,fieldTypeDictionary[fldType], fldPrecision, fldScale, fldLength) #adds field to data set

    updateTheRows = arcpy.UpdateCursor(updateDSParam) #get the rows from the shapefile that will have a field updated
    
    for updateTheRow in updateTheRows:
        #loop through all the rows in the update cursor
        if linkTypeText:
            whereClause = "'" + searchLinkParam + "' = '" + updateTheRow.getValue(updateLinkParam) + "'"
        else:
            whereClause = "'" + searchLinkParam + "' = " + updateTheRow.getValue(updateLinkParam) 

        if updateTheRow.getValue(updateLinkParam) == "01001":
            print updateTheRow.getValue(updateLinkParam)

            
        # THIS SECTION NOT WORKING...SEARCH CURSOR DOESN'T LOAD DATA (EVEN WITHOUT WHERECLAUSE)            
        searchTheRows = arcpy.SearchCursor(searchDSParam,whereClause)
        for searchTheRow in searchTheRows:
            if searchTheRow.getValue(searchFieldParam) <> "":
                updateTheRow.setValue(updateFieldParam,searchTheRow.getValue(searchFieldParam))
                updateTheRows.updateRow(updateTheRow)
                changed = changed + 1
                break #exit search cursor loop - you found first row with data and updated row
            
        searchTheRows = None
    updateTheRows = None
    
except:
    # Display exceptions
    if not arcpy.GetMessages() == "":
        arcpy.AddMessage(arcpy.GetMessage(2))

finally:
    #clean up everything
    if updateTheFields:
        del updateTheFields

    if searchTheFields:
        del searchTheFields

    if searchTheField:
        del searchTheField

    if searchTheRow:
        del searchTheRow

    if updateTheRow:
        del updateTheRow

    if updateTheRows:
        del updateTheRows

    if searchTheRows:
        del searchTheRows

    if searchDSParam:
        del searchDSParam

    if searchLinkParam:
        del searchLinkParam

    if updateDSParam:
        del updateDSParam

    if searchFieldParam:
        del searchFieldParam

    if updateLinkParam:
        del updateLinkParam

    if updateFieldParam:
        del updateFieldParam

    if fldLength:
        del fldLength

    if fldType:
        del fldType

    if fldPrecision:
        del fldPrecision

    if fldScale:
        del fldScale

    if whereClause:
        del whereClause

    if linkTypeText:
        del linkTypeText

    if fieldTypeDictionary:
        del fieldTypeDictionary
0 Kudos
TerrySilveus
Occasional Contributor III
So now I'm thinking it is my where clause... even though I'm sure I tested it without the where clause and got the same issue.  I decided to test without again, and this time it looks like it did load the search cursor.  So, below is what my where clause looks like as it is passes through the update cursor loop on an iteration that I know has a match... So what exactly should be surrounding the field name in the where clause for the search cursor?  'fldName' or "fldName" or !fldName! or [fldName] or something else?

This is my whereClause

"'CountyCode' = '01001'"
0 Kudos
LoganPugh
Occasional Contributor III
I'd suggest just writing a barebones version to make sure the actual SearchCursor part works and for other readers here to easily make sense of it. Rewriting it will probably help you figure out what went wrong the first time. Also you don't need to delete variables in Python with a few exceptions such as rows and cursors.
0 Kudos
DarrenWiens2
MVP Honored Contributor
I'm not totally sure, but I think the where clause switches you to SQL syntax. So, it would be:

CountryCode = '01001' <- all enclosed in double-quotes in SearchCursor

So,

whereClause = "CountryCode = '01001'"

That's really just my best guess - I'm learning, too. 🙂
0 Kudos
TerrySilveus
Occasional Contributor III
Thanks for the advice regarding deleting variables.  I will limit that to just deleting the cursor and row variables.  However, I'm not sure how much barer this code can be.  It's pretty slim as it is. 

it creates a field   (this part works)
it loads an update cursor (this part works)
it loads a search cursor (this part doesn't work) -when I use a where clause, but it works without the where clause, so there is an issue with my where clause.  I now think I just need to know how to wrap the field name.
0 Kudos
TerrySilveus
Occasional Contributor III
on another note... is there any way in python that I can just write an sql statement and use that instead or somehow pass an entire sql statement to the cursor?
0 Kudos
TerrySilveus
Occasional Contributor III
I'm not totally sure, but I think the where clause switches you to SQL syntax. So, it would be:

CountryCode = '01001' <- all enclosed in double-quotes in SearchCursor

So,

whereClause = "CountryCode = '01001'"

That's really just my best guess - I'm learning, too. 🙂


That's it.  You apparently don't need to wrap the field name for the where clause.  Thank you.
0 Kudos
MarkWiygul
New Contributor
Hi there.  I'm learning from this thread.  I'm understanding that the where clause is an SQL line, correct?  If that's the case and you need to provide a quoted expression as a parameter, and if a variable representing that expression is not quated, then you could use the...
repr() function with the variable as an argument to return extra quotes into the parameter as follows..
Thanks for posting your tests.  Let me know if you have any insight on what I'm figuring...

 
Python 2.5
IDLE 1.2.1  
>>>help(gp.searchcursor)           # gp = arcgisscripting.create(9.3) 
Help on built-in function searchcursor:

searchcursor(...)
    searchcursor(<dataset>, {where_clause}, {spatial_reference}, {fields}, {sort_fields}) -> object
    Returns a search cursor object against the given table/feature class.

>>> a = "anyStatement = '01001'"
>>> a
"anyStatement = '01001'"
>>> print a
anyStatement = '01001'
>>> print repr(a)
"anyStatement = '01001'"
>>> 


0 Kudos