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
Post a Comment