天问

pyodbc安装错误Linux python3 – Can’t open lib ‘SQL Server’解决

今天遇到一个朋友在windows中安装pyodbc成功,在CentOS中安装了,链接数据库的时候发现出现错误:

Linux python3 - Can't open lib 'SQL Server'

问题很简单,安装pyodbc前你需要安装Microsoft ODBC Driver for SQL Server

[code]

sudo su
zypper ar https://packages.microsoft.com/config/sles/12/prod.repo
exit
sudo ACCEPT_EULA=Y zypper install msodbcsql
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y zypper install mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo zypper install unixODBC-devel

[/code]

安装后,可以执行pip install pyodbc,并且odbc和pyodbc安装好了,测试一下:

[code]
import pyodbc
server = 'tcp:myserver.database.windows.net'
database = 'mydb'
username = 'myusername'
password = 'mypassword'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("SELECT @@version;")
row = cursor.fetchone()
while row:
print row
row = cursor.fetchone()
[/code]

博客地址:http://blog.yoqi.me/?p=4091
扫我捐助哦
喜欢 9036

这篇文章还没有评论

发表评论