会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132384个问题
Python 全系列/第一阶段:Python入门/Python入门(动画版) 17971楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 17972楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 17975楼
JAVA 全系列/(旧的隐藏)第二十一阶段:百战商城项目(Spring Cloud最新架构)/百战商城项目 17977楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 17978楼

import pymysql

class MyDb:
    config={
        "host":"localhost",
        "user":"root",
        "password":"123456",
        "db":"music_project",
        "charset":"utf8"
    }

    def __init__(self):
        self.connection = pymysql.connect(**DbUtils.config)
        self.cursor = self.connection.cursor()
    
    def close(self):
        if self.cursor:
            self.cursor.close()
        if self.connection:
            self.connection.close()

    #插入,修改,删除
    def exeDML(self,sql,*args):
        try:
            #执行sql
            count = self.cursor.execute(sql,args)
            id = self.connection.insert_id()
            #提交事务
            self.connection.commit()
            return id
        except Exception as e:
            print(e)
            if self.connection:
                self.connection.rollback()
        finally:
            self.close()

    #查询一条
    def queryone(self,sql,*args):
        try:
            #执行sql
            self.cursor.execute(sql,args)
            #获取结果集
            return self.cursor.fetchone()
        except Exception as e:
            print(e)
        finally:
            self.close()

    #查询多条
    def queryall(self,sql,*args):
        try:
            #执行sql
            self.cursor.execute(sql,args)
            #获取结果集
            return self.cursor.fetchall()    
        except Exception as e:
            print(e)
        finally:
            self.close()

if __name__ == "__main__":
    dbutils = MyDb()
    #测试exeDML
    # sql = "insert into emp(empno,ename,sal) values(%s,%s,%s)"
    # count = dbutils.exeDML(sql,6666,'阿巴阿巴',9999)
    # print(count)

    # sql = "select * from emp where empno=%s"
    # count = dbutils.queryone(sql,7788)
    # print(count)

    sql = "select * from t_music"
    emps = dbutils.queryall(sql)
    for i in emps:
        print(i)

图片.png

老师我也报了和评论一样的错误,可是我对照了一下资料的代码,发现好像一样啊,为什么会出现这种情况啊

Python 全系列/第五阶段:数据库编程/项目-音乐播放器-旧 17979楼
JAVA 全系列/(旧的隐藏)第二十一阶段:百战商城项目(Spring Cloud最新架构)/百战商城项目 17980楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术(旧) 17981楼
JAVA 全系列/第六阶段:项目管理与SSM框架/SpringMVC 17983楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 17984楼
JAVA 全系列/(旧的隐藏)第二十一阶段:百战商城项目(Spring Cloud最新架构)/百战商城项目 17985楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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