一、安裝環境
服務器:Windows Server 2016
Python版本:Python 3.8.5
服務器本地安裝有SQL Server 2008 R2
二、在python中安裝pymssql
D:\Python\Python38>pip install pymssql
Collecting pymssql
Downloading pymssql-2.1.5-cp38-cp38-win_amd64.whl (423 kB)
|████████████████████████████████| 423 kB 547 kB/s
Installing collected packages: pymssql
Successfully installed pymssql-2.1.5
三、用py程序測試連接
編寫以下testMSSQL.py代碼進行測試:
import pymssql
conn = pymssql.connect('(local)', 'sa', '123456', 'dbname') #建立連接
if conn:
print("連接成功!")
cursor = conn.cursor()
cursor.execute("select * from t_User")
for row in cursor:
print("UID=%d, ID=%s, Name=%s" % (row[0], row[1], row[2]))
cursor.close()
conn.close()
