python怎么连接sqlserver

如题所述

如果使用pyodbc,直接看2就可以了,可以略过1
1. Python连接mssql
ubuntu linux上
1.1
sudo apt-get install python
1.2 重启终端
1.3

sudo apt-get --assume-yes update
sudo apt-get --assume-yes install freetds-dev freetds-bin
sudo apt-get --assume-yes install python-dev python-pip
sudo pip install pymssql

1.4
sudo vi /etc/freetds/freetds.conf
2.安装pyodbc
2.1 为了解决pyodbc.h:52:17: fatal error: sql.h: No such file or directory

sudo yum install unixODBC-devel.x86_64
然后:
sudo pip install pyodbc
2.2 安装MSSQL native client
2.3

编写Python程序:
import pyodbc
conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};
SERVER=aaaa; UID=bbbb; PWD=ccc; DATABASE=ddd; Encrypt=yes;
TrustServerCertificate=no');
cursor = conn.cursor()
cursor.execute("select count(distinct d) as cnt from ddddd6 where day_id=20160531")
row = cursor.fetchone()
while row:
print str(row[0])
row = cursor.fetchone()
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-11-03
我们说了算(
相似回答