会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132359个问题
Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 301楼
Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 302楼
Python 全系列/第五阶段:数据库编程/项目-音乐播放器-旧 303楼

2.png

Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 304楼
Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 305楼
Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 306楼
Python 全系列/第五阶段:数据库编程/redis 307楼

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(旧) 308楼

1.png

Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 309楼
Python 全系列/第五阶段:数据库编程/redis 310楼
Python 全系列/第五阶段:数据库编程/redis 311楼
Python 全系列/第五阶段:数据库编程/redis 312楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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