会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132648个问题
Python 全系列/第五阶段:数据库编程/mysql的使用 589楼
Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 592楼
Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 593楼
Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 594楼
Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 595楼
Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 596楼
Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 598楼
Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 599楼

import pymysql

class dbUtil():
    def __init__(self):
        self.connect = pymysql.connect(host='localhost', user='root', passwd='root', db='playmusic', charset='utf8')
        self.cursor = self.connect.cursor()


    def close(self):
        if self.cursor:
            self.close()
        if self.connect:
            self.close()

    def exeDML(self, sql, *args):
        try:
            self.cursor.execute(sql, args)
            count = self.connect.commit()
            return count
        except Exception as e:
            print(e)
            if self.connect:
                self.connect.rollback()
        finally:
            self.close()

    def quryone(self, sql, *args):
        try:
            self.cursor.execute(sql, args)
            return self.cursor.fetchone()
        except Exception as e:
            print(e)
            if self.connect:
                self.connect.close()
        finally:
            self.close()

    def quryall(self, sql, *args):
        try:
            self.cursor.execute(sql, args)
            return self.cursor.fetchall()
        except Exception as e:
            print(e)
            if self.connect:
                self.connect.close()
        finally:
            self.close()

limage.png

老师以上代码会出现这个报错,我网上查了,说是超过最大递归深度,但是我写的另外一个一模一样的代码却可以正常运行,下面的这个代码是可以正常运行的。我检查了很多遍,实在找不出有什么区别

class dbUtil():
    def __init__(self):
        self.connect = pymysql.connect(host='localhost', user='root', passwd='root', db='playmusic', charset='utf8')
        self.cursor = self.connect.cursor()

    def close(self):
        if self.cursor:
            self.cursor.close()
        if self.connect:
            self.connect.close()

    def exeDML(self, sql, *args):
        try:
            self.cursor.execute(sql, args)
            count = self.connect.commit()
            return count
        except Exception as e:
            print(e)
            if self.connect:
                self.connect.rollback()
        finally:
            self.close()

    def quryone(self, sql, *args):
        try:
            self.cursor.execute(sql, args)
            # self.cursor.fetchone()
            return self.cursor.fetchone()
        except Exception as e:
            print(e)
            if self.connect:
                self.connect.close()
        finally:
            self.close()

    def quryall(self, sql, *args):
        try:
            self.cursor.execute(sql, args)
            return self.cursor.fetchall()
        except Exception as e:
            print(e)
            if self.connect:
                self.connect.close()
        finally:
            self.close()


Python 全系列/第五阶段:数据库编程/项目-音乐播放器-旧 600楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637