pyMySQL用类进行封装
class SqlHelper(object):
def __init__(self):
self.connect()
def connect(self):
self.conn = pymysql.connect(host="localhost", user="root", password="密码", db="studentmanage",
charset='utf8')
self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursors)
def get_list(self,sql,args):
self.cursor.execute(sql,args)
result = self.cursor.fetchall()
return result
def get_one(self,args):
self.cursor.execute(sql,args)
result = self.cursor.fetchone()
return result
def modify(self,sql):
self.cursor.execute(sql,args)
self.conn.commit()
def create(self,sql):
self.cursor.execute(sql,sql)
self.conn.commit()
return self.cursor.lastrowid
def close(self):
self.cursor.close()
self.conn.close()