Python read MySQL query result value standard output

Source: Internet
Author: User
Tags mysql query

#!/bin/python

#coding =utf-8

Import MySQLdb

Import time

Import Sys

Import Mysqldb.cursors

# add character set parameters to prevent Chinese garbled characters

Print "---------TOP OS SLAVE--------"

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>=subdat E (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 ()

Outputs the query results as an array.

This article is from the "Dream to Reality" blog, please be sure to keep this source http://lookingdream.blog.51cto.com/5177800/1917129

Python read MySQL query result value standard output

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.