[Tutor] Code not doing what I think it's doing

Kent Johnson kent37 at tds.net
Mon Nov 20 21:40:53 CET 2006


Todd Dahl wrote:
> So I have this piece of code below from my script that I am still 
> working on and it's not doing what my brain says it should.
>  
> If I remove the whole section starting at:
>  > for guid in guidlist:
>  
> it prints correctly the primary key of each row that I queried with the 
> line that starts with:
>  
>  > print "db_row: ", str(row[0])
>  
> but with the for statement line in it does not loop back to that 
> statement anymore and it makes no sence to me why not. Shouldn't it run 
> through the line:
>  
>  > print "db_row: ", str(row[0])
>  
> every single time? No matter if the for loop in there or not?

My wild guess is that the problem is that you are reusing the same 
cursor while you are iterating the result set. Try using a separate 
cursor for the nested update or read all the data from the result set 
into a list and iterate the list instead of the result set.

Kent
>  
> Please, excuse the littering of code snippets that I have been using for 
> trying to figure this out.
>  
> Thanks,
>  
> -Todd
>  
>  
> ==============================
>  
> 
> def parseRegistry():
>     """ Update database with entries that where found in the registry"""
>     #First collect all main registry keys under the CLSID tree and put 
> them into a list
>    
>     guidlist = ListRegistryKeys("CLSID")
>  
>     #Create database if it does not exist or open database if it does exist
>     #Create a connection object
>     connection = sqlite.connect('dcs2000.db ')
>    
>     #Create a cursor object
>     cursor = connection.cursor()
>    
>     #Loop through returned data
>     resultSet = cursor.execute("SELECT * FROM VBGuids where comType = 
> 'Object' ")
>     for row in resultSet:   
>         print "db_row: ", str(row[0])
>         #zz = raw_input("stop at ....db row")
>        
>         for guid in guidlist:
>             key2=_winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, guid)
>             j=0
>            
>             try:
>                 while 1:
>                     valueName, valueValue, valueType= 
> _winreg.EnumValue(key2, j)
>                     #print guid
>                     #print "valueName", valueName
>                     #print "valueValue", valueValue
>                     #print "valueType", valueType
>                     # zz = raw_input("... :-() ...")
>                    
>                     updateSQLString = "update VBGuids set 
> RegistrationPath='" + 'z:\lalala' + "', Name='" + 'NANA' + "' where id=" 
> + str(row[0])
>                     #print "SQL: ", updateSQLString
>                     #print "row: ", str(row[0])
>                    
>                     cursor.execute(updateSQLString)
>                     connection.commit()                   
>                    
>                    
>                     j += 1 
>                     #print j
>             except WindowsError, e:
>                 continue
>             except Exception, e:
>                 print '%s: %s' % (e.__class__.__name__, e)
>                 if isinstance(e, SystemExit): raise # take the exit
>             except:
>                 print 'Nonstandard Exception %r: %r' % 
> __import__('sys').exc_info()[:2]           
>                 sys.exit ()
>                
>             _winreg.CloseKey( key2)
> 
>  
>  
> 
>     # Okay we need to close the cursor and connection
>     cursor.close()
>     connection.close()
> 
>  
>  
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list