python - Mysql, open connection if not opened, close if opened -


how can check if mysql opened or closed? thanks

class myconnection:      def opendb(self):       if not self.db:                  self.db=mysqldb.connect(host="localhost",user="root"                         ,passwd="root",db="bloombergfinance")     def closedb(self):         if self.db:             self.db.close()      def addlistticker(self,insertlist):         try:            cur = self.db.cursor()            cur.execute('insert t...')              self.db.commit()         finally:             cur.close()  if __name__=="__main__":     mydb=myconnection()          mydb.opendb()      mydb.addlistticker(...)     mydb.closedb() 

this code gives error @ first time called, because python see self.db first time.

attributeerror: myconnection instance has no attribute 'db' 


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -