# MySql增删改 import pymysql # 创建连接对象的整段代码 conn = pymysql.connect(host='localhost', # 服务器的主机地址 port=3306, # mysql数据库的端口号 user='root', # 用户名 passwd='333333', # 密码 database='6yue', # 操作的数据库 charset='utf8', ) # 操作数据库使用的编码格式 cursor = conn.cursor() # 获取游标 目的是执行sql语句 # sql = "insert into shuju(姓名)values('大胖')" # 准备sql语句 增加语句 sql = "insert into shuju(姓名,手机号码,身份证号码,身高,体重)values(%s,%s,%s,%s,%s)" # 准备sql语句 增加语句 多条数据 # sql = "update shuju set 姓名='修改'where id =11;" # 准备sql语句 修改语句 # sql = "delete from shuju where id=53" # 准备sql语句 修改语句 删除语句 try: # cursor.execute(sql) # 执行sql语句 cursor.execute(sql, ['小胖', '2222', '3333', '199', '50']) # 执行sql语句 带多条数据 conn.commit() # 提交数据 except Exception as e: conn.rollback() # 对修改的数据进行撤销 表示数据的回滚 回到没有修改数据之前的状态 finally: cursor.close() # 关闭游标 conn.close() # 关闭连接
MySql增删改Python代码
最新推荐文章于 2024-06-12 19:31:55 发布