python怎么连接sqlserver
2个回答
展开全部
如果使用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. 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()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询