python连接mysql与方法的封装

 import pymysql
 
 class Model(object):
     def __init__(self, username='root', password='123456', database='demo', 
                  port=3306, host='localhost'):
         # 创建连接
         self.connection = pymysql.connect(user=username, password=password, database=database,
                                           port=port, host=host, cursorclass=pymysql.cursors.DictCursor)
         # 创建游标
         self.cursor = self.connection.cursor()
 
     # 查询所有数据
     def fetchall(self, sql):
         try:
             self.__execute(sql)
             return self.cursor.fetchall()
         except Exception as error:
             print(error)
 
     # 查询多条数据
     def fetchmany(self, sql, size=1):
         try:
             self.__execute(sql)
             return self.cursor.fetchmany(size)
         except Exception as error:
             print(error)
 
     # 查询一条数据
     def fetchone(self, sql):
         try:
             self.__execute(sql)
             return self.cursor.fetchone()
         except Exception as error:
             print(error)
 
     # 增删改的方法
     def change(self, sql):
         try:
             self.__execute(sql)
             self.connection.commit()
         except Exception as error:
             print(error)
 
     # 执行的私有方法
     def __execute(self, sql):
         self.cursor.execute(sql)
 
     # 关闭连接和游标
     def __del__(self):
         self.connection.close()
         self.cursor.close()
 from data_connect import model
 
 # 实例化Model类
 employee = model.Model()
 res = employee.fetchall('select nickname from employee where job="头领"')[0]
 res1 = employee.fetchmany('select nickname from employee where job="头领"', 2)
 res2 = employee.fetchone('select nickname from employee where name = "宋江"')
 # print(res2)
 
 # 插入一条语句
 employee.change('insert into employee (name)values ("关羽")')
 # 删除一条语句
 employee.change('delete from employee where name="关羽"')
 # 更新一条语句
 # obj.change('update employee set name="张飞"where id=8004')
 
原文地址:https://www.cnblogs.com/Liu928011/p/14773058.html