#-*-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 = "192.168.33.190"
user = "SA"
Password = "1111"
dbname = "TestDB"
Port = 1433
Def fetch_row ():
Try
#conn = Pymssql.connect (host,port,user,password,dbname,charset= "UTF-8", timeout=3)
conn = Pymssql.connect (server=host,port=port,database=dbname,user=user,password=password,charset= "UTF-8", timeout =3)
cursor = Conn.cursor ()
Sql= "Select Schema_id,name from T1"
Cursor.execute (SQL)
row = Cursor.fetchone () #相当于 Cursor.next method
While row:
Print ("id=%d, name=%s"% (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 = Pymssql.connect (server=host,port=port,database=dbname,user=user,password=password,charset= "UTF-8", timeout =3)
cursor = Conn.cursor ()
#sql = "INSERT INTO T1 SELECT * from T1"
sql= "Update t1 set object_id=object_id+1067705889 where name= ' Dm_resource_governor_resource_pool_volumes '"
Cursor.execute (SQL)
Conn.commit ()
Conn.close ()
Return 1
Except Exception,e:
Conn.rollback ()
Return E
Fetch_row ()
Change_row ()
Python for MSSQLserver