#-*-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