您好,登錄后才能下訂單哦!
本文主要給大家介紹python實(shí)現(xiàn)讀取mysql查詢結(jié)果值的輸出方式,文章內(nèi)容都是筆者用心摘選和編輯的,具有一定的針對性,對大家的參考意義還是比較大的,下面跟筆者一起了解下python實(shí)現(xiàn)讀取mysql查詢結(jié)果值的輸出方式吧。
#!/bin/python
#coding=utf-8
import MySQLdb
import time
import sys
import MySQLdb.cursors
# 加上字符集參數(shù),防止中文亂碼
print "---------TOP OS SLAVE 10--------"
conn = MySQLdb.connect(host="XXXX",db="mysql",user="user",passwd="password",port=338
0,charset="utf8")
cursor = conn.cursor()
query = "select distinct t.user_host,t.sql_text from mysql.slow_log t where t.query_time>5 and t.start_time>=SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')+3) and t.start_time<SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')-4) ORDER BY t.query_time DESC limit 10"
cursor.execute(query)
r = cursor.fetchall()
for a in r:
user_host =a[0]
sql_text =a[1]
print "\n"
print "%s\t%s" %(user_host,sql_text)
conn.close()
以數(shù)組形式輸出查詢結(jié)果。
函數(shù)形式多組查詢
#!/bin/python
#coding=utf-8
import MySQLdb
import time
import sys
import MySQLdb.cursors
def select(): #定義函數(shù)
query = "select distinct t.user_host,t.sql_text from mysql.slow_log t where t.query_time>5 and
t.start_time>=SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')+3) and t.start_time<SUBDATE(CURDATE(),D
ATE_FORMAT(CURDATE(),'%w')-4) ORDER BY t.query_time DESC limit 10"
cursor.execute(query)
r = cursor.fetchall()
for a in r:
user_host =a[0]
sql_text =a[1]
print "%s\t%s" %(user_host,sql_text)
print "\n"
conn.close()
print('\033[35m')
print "---------TOP OS SLAVE 10--------"
print('\033[0m')
conn=MySQLdb.connect(host="xxxxx",db="mysql",user="user",passwd="password",port=3380,charset="utf8")
cursor = conn.cursor()
select()
print('\033[35m')
print "---------TOP RETAIL SLAVE 10--------"
print('\033[0m')
conn=MySQLdb.connect(host="xxxxxx",db="mysql",user="user",passwd="password",port=3382,charset="utf8")
cursor = conn.cursor()
select()
看完以上關(guān)于python實(shí)現(xiàn)讀取mysql查詢結(jié)果值的輸出方式,很多讀者朋友肯定多少有一定的了解,如需獲取更多的行業(yè)知識信息 ,可以持續(xù)關(guān)注我們的行業(yè)資訊欄目的。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。