sql语句组件 在框架中的应用

class sqlHandler():
    def __init__(self):
        self.connect()

    def connect(self):
        self.conn = conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="", db="db2")
        self.cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

    def getOne(self,sql,args):
        self.cursor.execute(sql, args)
        result = self.cursor.fetchone()
        return result

    def getAll(self,sql,args):
        self.cursor.execute(sql, args)
        result = self.cursor.fetchall()
        return result

    def modify(self,sql,args):
        self.cursor.execute(sql,args)
        self.conn.commit()

    def modifyMany(self,sql,args):
        self.cursor.executemany(sql,args)
        self.conn.commit()

    def close(self):
        self.cursor.close()
        self.conn.close()

    def create(self,sql,args):
        self.cursor.execute(sql,args)
        ret = self.cursor.lastrowid
        self.conn.commit()
        return ret

 

posted on 2020-03-19 19:10  仙人小麦  阅读(189)  评论(0编辑  收藏  举报