会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题
Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 916楼
Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 918楼

from typing import Mapping
import pymysql


class DBUtil :
    config ={
        'host':'localhost',
        'user':'root',
        'password':'root',
        'db':'text01',
        '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 :
            count = self.cursor.execute(sql,args)
            self.connection.commit()
            
        except Exception as e :
            print(e)
            if self.connection:
                self.connection.rollback()
        finally :
            self.close()

    def query_one (self,sql,*args):
        try :
            count = self.cursor.execute(sql,args)
          
            return self.cursor.fetchone()
            
        except Exception as e :
            print(e)
            if self.connection:
                self.connection.rollback()
        finally:
            self.close()
    def query_all(self,sql,*args):
        try :
            self.cursor.execute(sql,args)
            emp = self.cursor.fetchall()
            return emp 
        except Exception as e :
            print(e)
            if self.connection:
                self.connection.rollback()
        finally:
            self.close()
if __name__ == '__mian__':
    dbutil = DBUtil()
    #sql = "insert into emp (empno,ename,sal) values(%s,%s,%s)"
    #dbutil.exedml(sql,9999,'lili',12000)
    sql = "select * from emp"
    emps = dbutil.query_all(sql)
    for e in emps:
        print(e,end="\n")
    

为什么运行完啥都没有


Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 922楼
Python 全系列/第五阶段:数据库编程/python操作mysql 923楼
Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 927楼
Python 全系列/第五阶段:数据库编程/mysql的使用 928楼
Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 929楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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