SQL Server,Python,在Mac OS上

时间:2016-11-24 08:35:10

标签: python sql-server macos unixodbc

首先,我知道这个问题曾被{/ 3}}询问/回答,但是我按照这里建议的步骤进行了操作,并且被困在了步骤的中间。

在提出问题之前,我有一个在CentOS服务器上运行良好的Python脚本:

import pyodbc 

server = 'tcp:192.168.1.1' 
database = 'MYDB' 
username = 'username' 
password = 'password'

sqlQuery = "SELECT * FROM dbo.DB1;"

cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute(sqlQuery)

相同的脚本在macOS上给我错误,所以我一步一步地遵循上一个here中的修改,我安装了 FreeTDS ,并使用tsql -S 192.168.1.1 -U username -P password进行了测试。到目前为止情况还不错。

然后我使用brew install unixodbc安装了 unixODBC ,并按照建议进行了配置:

/usr/local/Cellar/unixodbc/2.3.4/etc/odbcinst.ini

[FreeTDS]
Description=FreeTDS Driver for Linux & MSSQL on Win32
Driver=/usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsodbc.so
UsageCount=1

和..

/usr/local/Cellar/unixodbc/2.3.4/etc/odbc.ini

[MYSERVER]
Description         = Test to SQLServer
Driver              = FreeTDS
Trace               = Yes
TraceFile           = /tmp/sql.log
Database            = 'MYDB'
Servername          = 'tcp:192.168.1.1'
UserName            = 'username'
Password            = 'password'
Port                = 1433
Protocol            = 8.0
ReadOnly            = No
RowVersioning       = No
ShowSystemTables    = No
ShowOidColumn       = No
FakeOidIndex        = No   

然后我测试了..

$ isql -v MYSERVER
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[08S01][unixODBC][FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist
[01000][unixODBC][FreeTDS][SQL Server]Unknown host machine name.
[ISQL]ERROR: Could not SQLConnect

也..

$ isql -v tcp:192.168.1.1 username password
[IM002][unixODBC][Driver Manager]Data source name not found, and no default driver specified
[ISQL]ERROR: Could not SQLConnect

回到我在macOS上的python脚本,我改了一点,仍然无法连接到SQL服务器..

cnxn = pyodbc.connect('DRIVER=FreeTDS;SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute(sqlQuery)

有什么想法吗?

0 个答案:

没有答案