1、安装sqlserver

用的是sqlserver2012的版本

python连接sqlserver_python


网上很多,参考下

2、安装python3.7及以下环境

尝试安装python3.8的环境不能连接成功

conda create -n pytorch python=3.7

3、安装sqlserver的python包

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pymssql

4、新建数据库

用sqlserver管理工具

python连接sqlserver_sqlserver_02


连接数据库

python连接sqlserver_python_03


新建数据库

python连接sqlserver_开发语言_04


python连接sqlserver_sqlserver_05

5、可连接成功了

其中dbname就是上面创建的sa数据库名

# 连接数据库
    def getconn(self):
        return pymssql.connect(database=self.dbname,user=user,password=password,host=host,port=port)