会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132382个问题
Python 全系列/第六阶段:生产环境部署与协同开发/docker容器扩展 3456楼
Python 全系列/第十二阶段:Python_Django3框架/Django高级 3461楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/智能电话本项目实战 3463楼

import pymysql

from pymysql.cursors import Cursor

class dbutil:

    def __init__(self) -> None:

        self.connection=pymysql.connect(host="localhost",user="root",passwd="1658626287y",db="love",charset='utf8')

        self.cursor=self.connection.cursor()

    def close(self):

        if self.connection:

            self.connection.close()

        if self.cursor:

            self.cursor.close()

    def exedml(self,sql,*args):

        try:

            count=self.cursor.execute(sql,args)

            self.connection.commit()

            return count

        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:

            count=self.cursor.execute(sql,args)

            return self.cursor.fetchall()

        except Exception as e:

            print(e)

            if self.connection:

                self.connection.rollback()

        finally:

            self.close()

if __name__ ==  "__main__":

    dbutil1=dbutil()

    sql="insert into love(name,age,home,arm) values(%s,%s,%s,%s)"

    count=dbutil1.exedml(sql,7,21,13,69)

    print(count)

##老师你看你下我这段代码有没有啥问题

Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 3464楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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