Python operates mysql database (hundreds of databases and tables)
Problem description:
This afternoon to run a project db requirements, Baiku Baibai clean up dirty data, and then write a python script, after the run to inform the project, and then the project to the player a reward, the result is sad, all the mysql operations did not succeed (did not report an error, so I thought the implementation was successful).
Here is my python script, passing two files as parameters, host.txt and update.sql
Click (here) to collapse or open
#! / usr/bin/env python
#-*-coding: UTF-8-*-
Import sys
Import MySQLdb
Import datetime
Dbuser = 'xxx'
Dbpwd = 'xxx'
DB_NUM = 100
TB_NUM = 100
Args = sys.argv
If len (args)! = 3:
Print "USAGE:% s host.txt update.sql"% args [0]
Sys.exit ()
Start = datetime.datetime.now ()
Print "Start Time:% s"% start.strftime ('% Y-%m-%d% HRV% MRV% S')
Hostfile = args [1]
Sqlfile = args [2]
Rhost = open (hostfile)
Hostlist = rhost.readlines ()
Rhost.close ()
Rsqls = open (sqlfile)
Sqllist = rsqls.readlines ()
Rsqls.close ()
For host in hostlist:
Host = host.strip ()
Ip = host.split ('') [0]
Pt = host.split ('') [1]
Conn = MySQLdb.connect (ip,dbuser,dbpwd,port=int (pt))
Cursor = conn.cursor ()
Cursor.execute ('SET NAMES UTF8')
Sqls = []
For i in range (DB_NUM):
Sqls.append ("s" str (I) .zfill (2))
For sql in sqls:
Db=sql
# print 'ip=%s; port=%s; db=%s'% (ip,pt,db)
For j in range (TB_NUM):
If TB_NUM > 10:
J = str (j) .zfill (2)
For sql in sqllist:
Ct = sql.strip ()% (db,str (j)) cursor.execute (ct)
Print ct
Conn.close ()
End= datetime.datetime.now ()
Print "End Time:% s"% end.strftime ('% Y-%m-%d% HRV% MRV% S')
Update.sql is as follows:
Click (here) to collapse or open
Delete from XSY_%s.t_xsy_equip_cloth_%s where (clothid > = 201675 and clothid = 201725 and clothid