python3 连接SQLserver

时间:2022-08-18 04:22:23

pymssql 是Python语言用来连接微软 SQL SERVER 数据库的类库,实现了 Python DB API 2.0 。官网介绍

安装包:

(python34) C:\WINDOWS\system32> pip install  pymssql
Collecting pymssql
Downloading pymssql-2.1.-cp34-cp34m-win_amd64.whl (371kB)
% |████████████████████████████████| 378kB 525kB/s
Installing collected packages: pymssql
Successfully installed pymssql-2.1. (python34) C:\WINDOWS\system32>
(python34) C:\WINDOWS\system32>
(python34) C:\WINDOWS\system32> pip install pymysql
Collecting pymysql
Downloading PyMySQL-0.7.-py2.py3-none-any.whl (78kB)
% |████████████████████████████████| 81kB 155kB/s
Installing collected packages: pymysql
Successfully installed pymysql-0.7.

最简事例:

#!/usr/bin/env python
# encoding: utf-8
"""
@author: 侠之大者kamil
@file: sqlserver.py
@time: 2016/5/31 9:08
"""
import pymssql
conn = pymssql.connect(host="10.x.x.1x2\CRM_MY",user="scxxx",password="xxxx!@#",database="myerp_main_v5",charset="utf8")
cur = conn.cursor()
if not cur:
raise (NameError,"数据库连接失败")
cur.execute("SELECT top 30 * from p_Customer")
resList = cur.fetchall()#fetchall()是接收全部的返回结果行
conn.close()
print(resList)