一个简单的进行数据库操作的类,调用内部sqlzz函数并加入sql语句即可

import pymysql
#简单封装一个可以执行sql语句的类
class Mysql_text(object):
    #定义一个初始化函数用来打开数据库连接和创建游标对象
    def __init__(self):

        self.db = pymysql.connect('localhost','root','123456','py11')
        self.cursor = self.db.cursor()
    #这个函数用来执行sql语句
    def sqlzz(self,sql):
        self.cursor.execute(sql)
        self.db.commit()
    #当程序不再用到类的时候就关闭数据库链接
    def __del__(self):
        self.cursor.close()
        self.db.close()

猜你喜欢

转载自blog.csdn.net/yangbenhao/article/details/81712889