温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-11 19:08:51 来源:ITPUB博客 阅读:163 作者:Kind0f 栏目: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不支持事务
    
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI