1. 安装
官方链接: https://pypi.org/project/pymssql/
可以查看最新版本,和需求的python版本。
打开cmd,直接输入
pip install pymssql
结果:
Collecting pymssql
Downloading https://files.pythonhosted.org/packages/45/0c/81bf31194f4dc0ee06b429efe98ce2fc202876cd4749f95bddfb9f3b0a50/pymssql-2.1.4-cp37-cp37m-win_amd64.whl (411kB)
100% |████████████████████████████████| 419kB 1.3MB/s
Installing collected packages: pymssql
Successfully installed pymssql-2.1.4
You are using pip version 10.0.1, however version 20.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
然后顺手把pip版本更新了,毕竟他提示了嘛。
OK,安装搞定。
2. 悲伤的故事
然后我打开了pyCharm写下了一行代码
import pymssql
好的,报了红线,好像忘记添加依赖了啊
打开 File->Setting->Project Interperter,点击加号,搜索pymssql
,安装一下。
这里你会看到两个结果是因为我用了镜像的缘故,更换镜像的方式也很简单.
image.png
这里我用的是豆瓣的镜像.
3. 写段代码跑一下
import pymssql
conn = pymssql.connect(
host="xxxxxxxxxx",
user="xxxxxxxxxx",
password="xxxxxxxxxx",
database="xxxxxxxxxx")//数据库配置
sql = "select * from xxxxxxx"//sql语句
cursor = conn.cursor(as_dict=True)//注1
cursor.execute(sql)//执行
rooms = cursor.fetchmany(10)//获取结果集size = 10
conn.commit()
print(rooms)
cursor.close()//关闭
conn.close()
注1:
as_dict=True这个参数举个栗子说一下,如果是True,则返回 [{'student':'haha'}]
,如果为False那就不需要返回参数名称,为[{'haha'}]