python连接msSqlServer

#encoding:utf-8
import pymssql
# 需要用到pymssql组件 该组件下载地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql
conn = pymssql.connect(host="127.0.0.1",user="sa",password="123",database="Northwind")

#需要用到端口号时 
# conn = pymssql.connect(host="127.0.0.1",port="端口号",user="sa",password="123",database="Northwind")

#获取游标对象
cur=conn.cursor()

# #查询操作
sql="SELECT TOP 1000\
        [RegionID] ,\
        [RegionDescription]\
FROM    [Northwind].[dbo].[Region] \
	ORDER BY [RegionID] DESC"
cur.execute(sql)

list=cur.fetchall()
for data in list:
	print data[0],data[1]

if len(list)>0:
	maxid = list[0][0]

# #播入一条sql语句
sql="INSERT INTO [Northwind].[dbo].[Region]\
           ([RegionID]\
           ,[RegionDescription])\
     VALUES\
           ("+str(maxid+1)+"\
           ,'test')"
# #执行插入	
cur.execute(sql)
# #提交事务,不执行提交,数据插入不会生效
conn.commit()

# #关闭数据库连接
conn.close()

# #暂停
# raw_input()



   

你可能感兴趣的:(python连接msSqlServer)