Example #1
0
class mysqlPal:
    """docstring for ClassName"""
    def __init__(self):
        self.connect

    @property
    def connect(self, database='mysql'):
        if hasattr(self, 'conn'):
            try:
                self.conn.ping(True)
                self.cursor = self.conn.cursor(
                    cursorclass=MySQLdb.cursors.DictCursor)
            except Exception, e:
                self.close()
                self.connect

        if not hasattr(self, 'cursor'):
            #读取配置文件
            cf = ConfigParser.ConfigPar()
            host = cf.get(database, "host")
            user = cf.get(database, "user")
            passwd = cf.get(database, "password")
            db = cf.get(database, "database")
            port = cf.get(database, "port")
            #连接
            self.conn = MySQLdb.connect(host=host,
                                        user=user,
                                        passwd=passwd,
                                        db=db,
                                        port=int(port),
                                        charset="utf8")
            self.conn.autocommit(True)
            self.cursor = self.conn.cursor(
                cursorclass=MySQLdb.cursors.DictCursor)