Python for MySQL

Source: Internet
Author: User

#-*-Coding:utf-8-*-


‘‘‘
Python coded by

Written in 2016/8/31

Used for get win OS log for each Windows Server
‘‘‘

‘‘‘
pymssql Help Documentation
Http://pymssql.org/en/stable/pymssql_examples.html
‘‘‘


Import pymssql
Import MySQLdb
Import time


#全局变量
Host = "Sqlmonitorex.mysql.db.aa.com"
user = "AA"
Password = "AA"
dbname = "TestDB"
Port = 555


Def fetch_row ():
Try
#conn = Pymssql.connect (host,port,user,password,dbname,charset= "UTF-8", timeout=3)
conn = MySQLdb.connect (host,user,password,dbname,port=port,charset= "UTF8", connect_timeout=3)
cursor = Conn.cursor ()
sql = "SELECT * from Temp1;"
Cursor.execute (SQL)
row = Cursor.fetchone () #相当于 Cursor.next method
While row:
Print Row[0],row[1] #格式化输出

#time. Sleep (1) #每次输出等待1秒
row = Cursor.fetchone () #输出下一行 Cursor.next method

Conn.close ()
Return 1

Except Exception,e:
Return E

Def change_row ():
Try
conn = MySQLdb.connect (host,user,password,dbname,port=port,charset= "UTF8", connect_timeout=3)
cursor = Conn.cursor ()
#sql = "INSERT INTO Temp1 (id,name) VALUES (1, ' xxx ')"
sql= "Update temp1 set name= ' ZZZ ' where id=1 '
Cursor.execute (SQL)
Conn.commit ()
Conn.close ()
Return 1

Except Exception,e:
Conn.rollback ()
Return E


I=fetch_row ()
Print I
Change_row ()

Python for MySQL

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.