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

老师我这个老师运行不成功

image.png

import pymysql
class DBUtil:
    cinfig={
        'host':'localhost',
        'user':'root',
        'password':'199851',
        'db':'test01',
        'charset':'utf8'
    }
    def __init__(self):
        self. Connection = pymysql.connect(**DBUtil.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)
            #提交事务
            self0.connection.comit()
            return count
        except Exception as e:
            print(e)
            if self.Connection:
                self.Connection.rollback()
    def query_one(self,sql,*args):
        try:
            #执行sql
            self.cursor.execute(sql,args)
            #获取结果集
            return self.cursor.fetchone()
        except Exception as e:
            print(e)
        finally:
            self.close()
    def query_all(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__':
    dbutil = DBUtil()
    # sql = 'insert into emp (empno,ename,sal) values( %s,%s,%s)'
    # conut = dbutil.exeDML(sql,9999,'lili',12000)
    # print(conut)
    # sql = 'select * from emp where empno=%s'
    # emp = dbutil.query_one(sql,7788)
    # print(emp)
    sql = 'select * from emp'
    emps = dbutil.query_all(sql)
    for e in emps:
        print(e,end='\n')


Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 418楼
Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 419楼
Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 420楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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