python操作sql server查询数据


#encoding: utf-8
import pymssql
import sys
org = [''hk','hklh']
for i in org:
try:
conn = pymssql.connect(host="xxx",user="xxx",password="xxx",database="xxx",charset="UTF-8")
except pymssql.OperationalError, msg:
print "error: Could not Connection SQL Server!please check your dblink configure!"
sys.exit()
else:
cur = conn.cursor()
query="select * from [test].[dbo].[zsy] where org=%s" %i
cur.execute(query)
conn.commit
rows = cur.fetchall()
f=open('C:\Python27\out.txt','a')
print >>f,i
print >>f,rows
f.close()
cur.close()
conn.close()

附件:test


Whatever is worth doing is worth doing well.