您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python批量执行sql语句

bubuko 2022/1/25 19:07:50 python 字数 1176 阅读 853 来源 http://www.bubuko.com/infolist-5-1.html

注意:1、需要放到事务中,失败回滚2、记得commit3、结束后记得close连接# -*- coding: utf-8 -*- import pymysql.cursors # 连接数据库 connect = pymysql.Connect( host='localhost', port=3310 ...
注意:
1、需要放到事务中,失败回滚
2、记得commit
3、结束后记得close连接

# -*- coding: utf-8 -*- import pymysql.cursors # 连接数据库 connect = pymysql.Connect( host=‘localhost‘, port=3310, user=‘user‘, passwd=‘123‘, db=‘test‘, charset=‘utf8‘ ) # 事务处理 sql_1 = "UPDATE staff SET saving = saving + 1000 WHERE user_id = ‘1001‘ " sql_2 = "UPDATE staff SET expend = expend + 1000 WHERE user_id = ‘1001‘ " sql_3 = "UPDATE staff SET income = income + 2000 WHERE user_id = ‘1001‘ " try: cursor.execute(sql_1) # 储蓄增加1000 cursor.execute(sql_2) # 支出增加1000 cursor.execute(sql_3) # 收入增加2000 except Exception as e: connect.rollback() # 事务回滚 print(‘事务处理失败‘, e) else: connect.commit() # 事务提交 print(‘事务处理成功‘, cursor.rowcount) # 关闭连接 cursor.close() connect.close()

Python批量执行sql语句

原文:https://www.cnblogs.com/zyh0430/p/14764320.html


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶