python2.7 使用MySQLdb模組封裝一個獲取mysql連線的類案例
阿新 • • 發佈:2019-01-05
案例具體如下:
import MySQLdb
class GetMysql(object):def getCon(self):
Host="192.168.0.222"
Port=3306
User="root"
Passwd="111111"
Db="redmine"
try:
self.db=MySQLdb.connect(host=Host,user=User,Passwd=Passwd,db=Db,port=Port,charset='utf8')
except Exception as e:
print "Get Mysql Database Error!",('%s' % e)
return self.db
def closeCon(self):
try:
self.db.close()
except TypeError:
print "Close Mysql Database Error!",('%s' % e)