import pymysql
import os
import sys
db = pymysql.connect("10.33.85.145","root","Uxx_2013")
runsql = db.cursor()
runsql.execute("select version();")
version = runsql.fetchone()
print("DATABASE VERSION : ",version)
runsql.execute("show databases;")
databases = runsql.fetchall()
seq = 0
for database in databases:
database = "".join(tuple(database))
runsql.execute("use %s;" % database)
runsql.execute("show tables;")
tables = runsql.fetchall()
for table in tables:
table = "".join(tuple(table))
runsql.execute("use %s;" % database)
try:
runsql.execute("select count(1) from %s" % table)
except Exception as ep:
print("Error Msg" + str(ep))
continue
tablecount = runsql.fetchone()
tablecount = int("".join('%d' % id for id in tablecount))
if tablecount > 0:
runsqlDic = db.cursor(cursor=pymysql.cursors.DictCursor)
runsqlDic.execute("select * from %s limit 1" % table)
selectOne = runsqlDic.fetchone()
else:
selectOne = '{Null}'
print("序列: {0:<5} 数据库: {1:<18} 表: {2:<55} 数据量: {3:<5} 抽取数据: {4}".format(seq,database,table,tablecount,selectOne))
with open("exp.txt",'a+') as fb:
if seq == 0:
fb.seek(0)
fb.truncate()
seq += 1
else:
fb.write("序列: {0:<5} 数据库: {1:<18} 表: {2:<55} 数据量: {3:<5} 抽取数据: {4}".format(seq,database,table,tablecount,selectOne) + '\n')
seq += 1
print("Write is Over !")
db.close()
序列: 1 数据库: information_schema 表: COLLATIONS 数据量: 222 抽取数据: {'COLLATION_NAME': 'big5_chinese_ci', 'CHARACTER_SET_NAME': 'big5', 'ID': 1, 'IS_DEFAULT': 'Yes', 'IS_COMPILED': 'Yes', 'SORTLEN': 1}
序列: 2 数据库: information_schema 表: COLLATION_CHARACTER_SET_APPLICABILITY 数据量: 222 抽取数据: {'COLLATION_NAME': 'big5_chinese_ci', 'CHARACTER_SET_NAME': 'big5'}