Linux下Python通过pyodbc连接SQLServer, Mark一下。
安装unixODBC
yum install unixODBC*
安装freetds
yum install freetds*
检查安装列表
[root@getway ~]# rpm -qa | grep unixODBC unixODBC-devel-2.2.14-14.el6.x86_64 unixODBC-kde-2.2.14-14.el6.x86_64 unixODBC-2.2.14-14.el6.x86_64 [root@getway ~]# rpm -qa | grep freetds freetds-devel-0.91-2.el6.x86_64 freetds-0.91-2.el6.x86_64 freetds-doc-0.91-2.el6.x86_64
安装pyodbc
git clone https://github.com/mkleehammer/pyodbc.git
cd pyodbc
pip install .
配置ODBC
vi /etc/odbcinst.ini
[SQLServer]
Description = FreeTDS ODBC driver for MSSQL
Driver = /usr/lib64/libtdsodbc.so
Setup = /usr/lib64/libtds.so
FileUsage = 1
PS:Driver和Setup的路径可用下面的命令查看:
rpm -ql unixODBC-devel-2.2.14-14.el6.x86_64
使用Python测试连接
import pyodbc conn=pyodbc.connect('DRIVER={SQLServer};SERVER=127.0.0.5;port=1433;DATABASE=lxw1234;UID=sa;PWD=lxw1234.com') cursor=conn.cursor() for row in cursor.execute("select TOP 50 * from t_lxw1234_fact"): print(row)
PS:这里指定的DRIVER,就是上面在odbcinst.ini中配置的ODBC驱动名称.
如果觉得本博客对您有帮助,请 赞助作者 。