1. 程式人生 > >python2.7 使用MySQLdb模組封裝一個獲取mysql連線的類案例

python2.7 使用MySQLdb模組封裝一個獲取mysql連線的類案例

案例具體如下:

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)