一、安装环境

服务器:Windows Server 2016

Python版本:Python 3.8.5

服务器本地安装有SQL Server 2008 R2

 

二、在python中安装pymssql

D:PythonPython38>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()

  

内容来源于网络如有侵权请私信删除