关闭

Python 连接SQLSERVER

569人阅读 评论(0) 收藏 举报
分类:

如果使用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()

Can't open lib '/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0' : file not found (0) (SQLDriverConnect)"

方案:ldd /opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0

    linux-vdso.so.1 =>  (0x00007fff869fe000)
    libcrypto.so.6 => not found
    libdl.so.2 => /lib64/libdl.so.2 (0x00007f360e269000)
    librt.so.1 => /lib64/librt.so.1 (0x00007f360e061000)
    libssl.so.6 => not found
    libuuid.so.1 => /lib64/libuuid.so.1 (0x00007f360de5b000)
    libodbcinst.so.1 => /lib64/libodbcinst.so.1 (0x00007f360dc44000)
    libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007f360d964000)
    libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007f360d719000)
    libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f360d411000)
    libm.so.6 => /lib64/libm.so.6 (0x00007f360d10f000)
    libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f360cef8000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f360ccdc000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f360c91a000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f360e7bd000)
    libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007f360c6e4000)
    libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007f360c4e0000)
    libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007f360c2d2000)
    libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007f360c0cd000)
    libresolv.so.2 => /lib64/libresolv.so.2 (0x00007f360beb3000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f360bc8d000)
    libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f360ba2c000)
    liblzma.so.5 => /lib64/liblzma.so.5 (0x00007f360b807000)

 libcrypto.so.6和 libssl.so.6不存在,sudo yum install openssl098e,再次ldd /opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0,上述依赖包已经安装成功。

问题解决


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:53378次
    • 积分:1271
    • 等级:
    • 排名:千里之外
    • 原创:58篇
    • 转载:34篇
    • 译文:15篇
    • 评论:3条
    文章分类
    最新评论