pymysql의 삭제 수정 도구 클래스

# encoding = utf8
import pymysql
class PymysqlUtil():
    # 
    def __init__(self,host,port,user,passwd,dbName,charsets):
        self.host = host
        self.port = port
        self.user = user
        self.passwd = passwd
        self.dbName = dbName
        self.charsets = charsets
    # 
    def getCon(self):
        self.db = pymysql.Connect(
            host=self.host,
            port=self.port,
            user=self.user,
            passwd=self.passwd,
            db=self.dbName,
            charset=self.charsets
        )
        self.cursor = self.db.cursor()
    # 
    def close(self):
        self.cursor.close()
        self.db.close()
    # 
    def get_one(self,sql):
        res = None
        try:
            self.getCon()
            self.cursor.execute(sql)
            res = self.cursor.fetchone()
        except:
            print(" !")
        return res
    # 
    def get_all(self,sql):
        res = None
        try:
            self.getCon()
            self.cursor.execute(sql)
            res = self.cursor.fetchall()
            self.close()
        except:
            print(" !")
        return res
    # 
    def __insert(self,sql):
        count = 0
        try:
            self.getCon()
            count = self.cursor.execute(sql)
            self.db.commit()
            self.close()
        except:
            print(" !")
            self.db.rollback()
        return count
    # 
    def __edit(self,sql):
        return self.__insert(sql)
    # 
    def __delete(self,sql):
        return self.__insert(sql)
    # 
    def __update(self,sql):
        return self.__insert(sql)

좋은 웹페이지 즐겨찾기