python for MSSQLserver
Posted 两年十一时
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章