如何检查mysql是打开还是关闭? 感谢
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 into T...')
self.db.commit()
finally:
cur.close()
if __name__=="__main__":
mydb=MyConnection()
mydb.openDb()
mydb.addListTicker(...)
mydb.closeDb()
这段代码在第一次调用时会出错,因为python第一次看到self.db。
AttributeError: MyConnection instance has no attribute 'db'