免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1541 | 回复: 0

使用python脚本操作mysql数据库时如何避免使用事务 [复制链接]

论坛徽章:
5
程序设计版块每日发帖之星
日期:2016-04-15 06:20:00每日论坛发贴之星
日期:2016-04-15 06:20:0015-16赛季CBA联赛之八一
日期:2016-07-08 09:20:28操作系统版块每日发帖之星
日期:2016-08-03 06:20:002016科比退役纪念章
日期:2016-10-30 13:59:12
发表于 2017-04-20 17:43 |显示全部楼层
#coding=utf-8
import MySQLdb

conn = MySQLdb.connect(host='localhost',port = 3306,user='admin',passwd='admin',db='google')
cur = conn.cursor()
try:
    create_tb_cmd='''
    create table if not exists tmp.Video_2017bak
    (id int(10) unsigned,
    asset_id int(10) unsigned,
    company_id int(10),
     );
    '''
    cur.execute(create_tb_cmd)
finally:
    insert_tb_cmd='''
    insert ignore into tmp.Video_2017bak (select * from google.Video where created_at < date_sub(now(), interval 2 year));'''
    delete_tb_cmd='''
    delete from google.Video where created_at < date_sub(now(), interval 2 year);'''
    cur.execute(insert_tb_cmd)
    cur.execute(delete_tb_cmd)

cur.close()
conn.commit()
conn.close()

我想问一下在上面的语句中,conn.commit()是不是把上面的sql语句做为事务来提交?

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP