首页 > 代码库 > python for MSSQLserver

python for MSSQLserver

# -*- coding: utf-8 -*-


‘‘‘
python coded by 

written in 2016/8/31

Used for get win os log for each windows server
‘‘‘

 

‘‘‘
pymssql 帮助文档
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方法
while row:
print("ID=%d, Name=%s" % (row[0], row[1])) #格式化输出

#time.sleep(1) #每次输出等待1秒
row = cursor.fetchone() #输出下一行 cursor.next方法
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