I'm running a portion of code in a loop. It accesses a global variable only to print it, and that variable is never changed after it is set in the __init__. However, the first print statement occasionally will fail, but the second one does not. Any ideas as to why this is happening? The method is below:

EDIT: it's not the print statement that is failing.
Sometimes the except statement runs for no apparent reason even after the self.cursor.execute completes

def db_insert(self,query_string,*params):
        try:
            self.cursor.execute(query_string, params)
            print ("Inserted into: "+self.database+".") 
        except:
            print("!!!!WARNING!!!! Insertion into "+self.database+" failed.\n")

EDIT: it's not the print statement that is failing.
Sometimes the except statement runs for no apparent reason even after the self.cursor.execute completes

Which means it is the print statement that is causing the error. Break it up to see which part is the problem (which should make it obvious since you are only printing one variable). Without a proper error message there is no way to debug, so note the addition of the traceback.

def db_insert(self,query_string,*params):
        try:
            self.cursor.execute(query_string, params)
            print ("Inserted into: "),
            print(self.database) 
        except:
            import traceback
            traceback.print_exc()
            raise
            print("!!!!WARNING!!!! Insertion into "+self.database+" failed.\n")

Actually, it was not a print error. The cursor.execute was running, but failing out. However, it was difficult to notice because the fail was due to a duplication. I had to print a traceback.format_exc() to find the error. It's working fine now. Thanks for the help.

poeticinsanity, thanks for letting us know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.