Comparing values in an Access table with filenames of existing GDB's

309
2
02-18-2011 04:40 AM
RachelWilliams
New Contributor II
I am trying to use a lookup table to compare a list of facilities that need geodatabases with the geodatabases that already exist in order to generate a list of geodatabases that need to be created. My script is stopping after the first record in the table and does not continue to search through any subsequent records. Here is what I have so far:


lookuptbl = r"P:\\USACE Mobile 0610-01 2010 63D RSC NR Contract\\03-Source Documents\\GIS\\SITE_ID_LOOKUP.mdb\\new_facility"
q = '[include_in_gis] = ' + "'" + "X" + "'"
#prnt q
#IF q is satisfied, the gdb that is currently being processed was found in lookup table;
#If q is NOT satisfied, the script will skip the gdb alltogether; nothing will be done


rows = gp.SearchCursor(lookuptbl, q)
row = rows.next()

while row:
    facil_id = row.GetValue("facil_id")
    print facil_id
    lookuptbl2 = r"P:\\USACE Mobile 0610-01 2010 63D RSC NR Contract\\03-Source Documents\\GIS\\SITE_ID_LOOKUP.mdb\\gdbs"
    q2 = '[facil_id_in_gdbs] = ' + "'" + facil_id + "'"
    #prnt q
    #IF q is satisfied, the gdb that is currently being processed was found in lookup table;
    #If q is NOT satisfied, the script will skip the gdb alltogether; nothing will be done
   
    rows = gp.SearchCursor(lookuptbl2, q2)
    row = rows.next()
    
    while row:
        print facil_id + "--already exists"
        row = rows.next()
    
        
    row = rows.next()        


I am new to writing scripts so any help you could give me would be much appreciated.
Tags (2)
0 Kudos
2 Replies
NiklasNorrthon
Occasional Contributor III
Your second cursor overwrite and destroys the first one. Your loops should be for loops instead. Don't complicate the queries unnecearily. Take advantage of python's alternative quoting syntaxes. And use format strings.

# Untested, fixing syntax errors is left as an exercise for the reader
lookuptbl = 'whatever'
q = "[include_in_gis] = 'X' "

lookuptbl2 = 'whatever_else'
q2 = "[facil_id_in_gdbs] = '%s' " # or %d depending on the datatype of facil_id

rows = gp.SearchCursor(lookuptbl, q)
for row in iter(rows.next, None): # This is magic to converts a cursor to an iterator
    facil_id = row.GetValue('facil_id')
    print facil_id

    rows2 = gp.SearchCursor(lookuptbl2, q2 % facil_id)
    for row2 in iter(rows2.next, None): # More magic...
        print facil_id + "--already exists"
    
row2 = None
rows2 = None
row = None
rows = None
0 Kudos