python操作mysql数据库(百库百表)

问题描述:
    今天下午跑某项目db需求,百库百表清脏数据,然后自己写了个python脚本,跑完之后通知项目,然后项目给玩家发奖励了,结果悲催了,所有的mysql操作没有执行成功(没有报错,因而以为执行成功)。

以下是我的python脚本,传两个文件作为参数,host.txt 以及 update.sql
    

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. -*- coding: UTF--*-


  3. import sys
  4. import MySQLdb
  5. import datetime



  6. dbuser = 'xxx'
  7. dbpwd = 'xxx'

  8. DB_NUM = 100
  9. TB_NUM = 100
  10. args = sys.argv
  11. if len(args)!=3:
  12.                 print "USAGE: %s host.txt update.sql" % args[0]
  13.                 sys.exit()

  14. start = datetime.datetime.now()
  15. print "Start Time : %s " % start.strftime('%Y-%m-%d %H:%M:%S')

  16. hostfile = args[1]
  17. sqlfile = args[2]

  18. rhost = open(hostfile)
  19. hostlist = rhost.readlines()
  20. rhost.close()


  21. rsqls = open(sqlfile)
  22. sqllist = rsqls.readlines()
  23. rsqls.close()

  24. for host in hostlist:
  25.                 host = host.strip()
  26.                 ip = host.split(' ')[0]
  27.                 pt = host.split(' ')[1]
  28.                 conn = MySQLdb.connect(ip,dbuser,dbpwd,port=int(pt))
  29.                 cursor = conn.cursor()
  30.                 cursor.execute('SET NAMES UTF8')

  31.                 sqls = []
  32.                 for i in range(DB_NUM):
  33.                         sqls.append("%s" % str(i).zfill(2))
  34.                 for sql in sqls:
  35.                         db=sql
  36.                         #print 'ip=%s ; port=%s ; db=%s' % (ip,pt,db)
  37.                         for j in range(TB_NUM):
  38.                                 if TB_NUM > 10:
  39.                                         j = str(j).zfill(2)
  40.                                 for sql in sqllist:
  41.                                         ct = sql.strip() % (db,str(j))
  42.                                         cursor.execute(ct)
  43.                                         print ct

  44.                 conn.close()
  45. end= datetime.datetime.now()
  46. print "End Time : %s " % end.strftime('%Y-%m-%d %H:%M:%S')

update.sql如下:

点击(此处)折叠或打开

  1. delete from XSY_%s.t_xsy_equip_cloth_%where (clothid >= 201675 and clothid <= 201700) or (clothid >= 201725 and clothid <= 201751);
host.txt如下:

点击(此处)折叠或打开

  1. 192.168.xx.xx 3306
每执行一条语句都print出来,结果是成功打印出了10000条,但是上库看binlog日志并没有任何记录,数据也没有变化

解决:
    临时写了个shell脚本,跑完需求,成功执行。
    其中传入的$1为包含10000条操作的delete语句文件

点击(此处)折叠或打开

  1. #!/bin/bash
  2. user=xxx
  3. pwd=xxx
  4. while read sql
  5. do
  6. echo $sql
  7. /usr/bin/mysql -h"192.168.xxx.xx" -P3306 -utab -ptab -e "${sql}"
  8. done < $1



发现问题:

    开始以为自己远端没有执行,然后重新在test库上使用以上python脚本create百表,登上去查看,发现操作成功。
    但是insert、update、以及delete操作无效。

    查资料得知,mysql的ddl操作是隐式提交的,而dml是需要显示commit才能提交 成功,而我的这个python脚本没有调用cursor.commit(),故而close之后并没有提交相应的操作。

    这里要区分shell中连接执行和python MySQLdb模块中连接执行的区别:
    
    ①shell中写的是连接到库上默认autocommit是开启的,故而执行时自动提交的,
    ②而pyrhon的该模块在dml操作之后调用 conn.commit()显示提交或者在连接mysql之后执行设置conn.autocommit(1)。这是由于MySQLdb模块在连接mysql之后关闭了自动提交。需要注意的是这里针对的是innodb存储引擎,因为mysaim不支持事务
    

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31385648/viewspace-2125719/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31385648/viewspace-2125719/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值