Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python operates mysql database (hundreds of databases and tables)

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report