我是靠谱客的博主 幸福小猫咪,最近开发中收集的这篇文章主要介绍python中关于mysql的封装(课堂),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 

 

 

代码:

class MySqlTools:
    def __init__(self,host,user,password,database,port=3306,charset = "utf8"):
        self.host = host
        self.user = user
        self.password = password
        self.database = database
        self.port = port
        self.charset = charset

    def __connet(self):
        self.conn = None
        self.cursor = None
        self.conn = pymysql.connect(host=self.host,
                                    user=self.user,
                                    password=self.password,
                                    database=self.database,
                                    port=self.port,
                                    charset=self.charset
                                    )
        self.cursor = self.conn.cursor()
        def __cud(self,sql,params=[]):
            """

            :param self:
            :param sql:
            :param params:
            :return:
            """
            try:
                self.__connet()
                self.cursor.execute(sql,params)
                self.conn.commit()
            except Exception as e:
                print(e)
                self.conn.rollback()
            finally:
                self.__close()

        def __close(self):
            """
            释放资源
            :param self:
            :return:
            """
            if self.cursor:
                self.cursor.close()
            if self.conn:
                self.conn.close()

    def insert(self,sql,paeams = []):
        """

        :param sql:
        :param paeams:
        :return:
        """
        self.__cud(sql,params)

    def update(self,sql,params = []):
        """
        delete操作
        :param sql:
        :param params:
        :return:
        """
        self.__cud(sql,params)

        def getOne(self,sql,params=[]):
            """

            :param self:
            :param sql:
            :param params:
            :return:
            """
            result = None
            try:
                self.__connet()
                self.cursor.execute(sql,paeams)
                result =self.cursor.fetchall()
            except Exception as e:
                print(e)
            finally:
                self.__close()
            return  result
 

最后

以上就是幸福小猫咪为你收集整理的python中关于mysql的封装(课堂)的全部内容,希望文章能够帮你解决python中关于mysql的封装(课堂)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部