1.安装pymssql-2.0.0b1.win32-py2.7.exe
http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql
2.连接数据库
from pymssql import *
con =connect(server='127.0.0.1',user='sa',password='123',database='TEST',port='1433')
3 .执行命令
cur=con.cursor()
strsql=‘SELECT * FROM test1;'
cur.execute(strsql)
4 打印结果
row=cur.fetchall()
print row
5调用过程记得提交,否则插入等语句不生效
con.commit()
也可直接设置自动提交
con.autocommit(True)
PS:
1.由于TDS 原因,用命令cmd执行正常,用Python Shell执行会报错
OperationalError: (20017, '\xc4DB-Lib error message 20017, severity 9:\nUnexpected EOF from the server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n')
http://w.huoliquankai.info/browse.php?u=1e674633b2e8015923Oi8vcHl5b3Uud29yZHByZXNzLmNvbS8yMDA5LzAyLzAxL2luc3RhbGwtcHltc3NxbC1hbmQtaG93LXRvLWRlYWwtd2l0aC1kYi1saWItZXJyb3ItbWVzc2FnZS0yMDAwOS1zZXZlcml0eS05Lw%3D%3D&b=0&f=norefer
2.2.7 的帮助功能Module Docs不错,可以方便查看模块定义,包括pymssql等后期加入的模块