# -*- conding:utf-8 -*-
import pymysql
import os
import sys
# 開啟資料庫連線
db = pymysql.connect(
"10.33.85.145"
,"root"
,"uxx_2013"
)# 使用 cursor() 方法建立乙個游標物件 cursor
runsql = db.cursor(
)# 使用 execute() 方法執行 sql 查詢
runsql.execute(
"select version();"
)# 使用 fetchone() 方法獲取單條資料.
version = runsql.fetchone(
)print
("database version : "
,version)
#獲取庫
runsql.execute(
"show databases;"
)databases = runsql.fetchall(
)#初始化seq (用於清空檔案歷史資料)
seq =
0for database in databases:
database =
"".join(
tuple
(database)
)#格式化tuple為str
#獲取所有表(查詢每個表一條資料,寫入檔案)
runsql.execute(
"use %s;"
% database)
runsql.execute(
"show tables;"
) tables = runsql.fetchall(
)for table in tables:
table =
"".join(
tuple
(table)
)#格式化tuple為str
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
forid
in tablecount)
)#格式化tuple為int
if tablecount >0:
runsqldic = db.cursor(cursor=pymysql.cursors.dictcursor)
#輸出資料為字典
runsqldic.execute(
"select * from %s limit 1"
% table)
selectone = runsqldic.fetchone(
)#selectone = list(selectone)
else
: selectone =
''print
("序列: 資料庫: 表: 資料量: 抽取資料: "
.format
(seq,database,table,tablecount,selectone)
)with
open
("exp.txt"
,'a+'
)as fb:
if seq ==0:
# os.remove('exp.txt')
fb.seek(0)
#定位到開頭
fb.truncate(
)#清空資料
seq +=
1else
: fb.write(
"序列: 資料庫: 表: 資料量: 抽取資料: "
.format
(seq,database,table,tablecount,selectone)
+'\n'
) seq +=
1print
("write is over !"
)# 關閉資料庫連線
db.close(
)
序列:
1 資料庫: information_schema 表: collations 資料量:
222 抽取資料:
序列:222 抽取資料:
shell查詢MySQL並將結果寫入檔案中
找到你的php.ini檔案,一般在 usr local php etc php.ini,開啟 查詢到 disable functions 刪除需要使用的函式名,如下 disable functions phpinfo,eval,passthru,exec,system,chroot,scandir,...
解析檔案並寫入檔案裡
ifndef analys h define analys h include include object.h define fgetsmax 6 1024 1024 獲取一行資料的最大值6m class object class analys object char v,int l value ...
監聽Log 並寫入檔案
public class notelog public static void stopnotelog private static void startnotelogthread catch ioexception e finally readlogthread.start private sta...