2013-07-08 126 views
0

我已經使用Homebrew安裝了freetds。我的配置如下:在Mac OS X上無法使用isql連接到SQL Server

odbcinst -j:

unixODBC 2.3.1
DRIVERS............: /usr/local/Cellar/unixodbc/2.3.1/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/Cellar/unixodbc/2.3.1/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/Cellar/unixodbc/2.3.1/etc/ODBCDataSources
USER DATA SOURCES..: /Users/mark.richman/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

/usr/local/Cellar/unixodbc/2.3.1/etc/odbcinst.ini:

[ODBC]
Trace = Yes
TraceFile = /tmp/odbc.log

/usr/local/Cellar/freetds/0.91/etc/freetds.conf:

[db.mydomain.local]
host = db.mydomain.local
port = 1433
tds version = 8.0
client charset = UTF-8

~/.odbc.ini:

[MomImporterDSN]
Description = Mom Importer
Driver = /usr/local/Cellar/freetds/0.91/lib/libtdsodbc.so
Servername = db.mydomain.local
Database = MomImporter
UserName = mark.richman
Password = mypassword

當我嘗試isql -v MomImporterDSN我得到以下錯誤:

[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[01000][unixODBC][FreeTDS][SQL Server]Adaptive Server connection failed
[ISQL]ERROR: Could not SQLConnect

/tmp/odbc.log似乎並不特別有用:

[ODBC][14514][1373290079.248688][__handles.c][460] 
     Exit:[SQL_SUCCESS] 
      Environment = 0x7fcbe1004c00 
[ODBC][14514][1373290079.249314][SQLAllocHandle.c][375] 
     Entry: 
      Handle Type = 2 
      Input Handle = 0x7fcbe1004c00 
[ODBC][14514][1373290079.249470][SQLAllocHandle.c][493] 
     Exit:[SQL_SUCCESS] 
      Output Handle = 0x7fcbe1024800 
[ODBC][14514][1373290079.249712][SQLConnect.c][3700] 
     Entry: 
      Connection = 0x7fcbe1024800 
      Server Name = [MOMImporterDSN][length = 14 (SQL_NTS)] 
      User Name = [NULL] 
      Authentication = [NULL] 
     UNICODE Using encoding ASCII 'char' and UNICODE 'UCS-2-INTERNAL' 

     DIAG [01000] [FreeTDS][SQL Server]Adaptive Server connection failed 

     DIAG [S1000] [FreeTDS][SQL Server]Unable to connect to data source 

[ODBC][14514][1373290079.703504][SQLConnect.c][4067] 
     Exit:[SQL_ERROR] 
[ODBC][14514][1373290079.704257][SQLError.c][434] 
     Entry: 
      Connection = 0x7fcbe1024800 
      SQLState = 0x7fff5a823526 
      Native = 0x7fff5a823520 
      Message Text = 0x7fff5a823530 
      Buffer Length = 500 
      Text Len Ptr = 0x7fff5a82351e 
[ODBC][14514][1373290079.704559][SQLError.c][471] 
     Exit:[SQL_SUCCESS] 
      SQLState = S1000 
      Native = 0x7fff5a823520 -> 0 
      Message Text = [[unixODBC][FreeTDS][SQL Server]Unable to connect to data source] 
[ODBC][14514][1373290079.704777][SQLError.c][434] 
     Entry: 
      Connection = 0x7fcbe1024800 
      SQLState = 0x7fff5a823526 
      Native = 0x7fff5a823520 
      Message Text = 0x7fff5a823530 
      Buffer Length = 500 
      Text Len Ptr = 0x7fff5a82351e 
[ODBC][14514][1373290079.704996][SQLError.c][471] 
     Exit:[SQL_SUCCESS] 
      SQLState = 01000 
      Native = 0x7fff5a823520 -> 20002 
      Message Text = [[unixODBC][FreeTDS][SQL Server]Adaptive Server connection failed] 
[ODBC][14514][1373290079.705172][SQLError.c][434] 
     Entry: 
      Connection = 0x7fcbe1024800 
      SQLState = 0x7fff5a823526 
      Native = 0x7fff5a823520 
      Message Text = 0x7fff5a823530 
      Buffer Length = 500 
      Text Len Ptr = 0x7fff5a82351e 
[ODBC][14514][1373290079.705357][SQLError.c][471] 
     Exit:[SQL_NO_DATA] 
[ODBC][14514][1373290079.705583][SQLError.c][514] 
     Entry: 
      Environment = 0x7fcbe1004c00 
      SQLState = 0x7fff5a823526 
      Native = 0x7fff5a823520 
      Message Text = 0x7fff5a823530 
      Buffer Length = 500 
      Text Len Ptr = 0x7fff5a82351e 
[ODBC][14514][1373290079.705763][SQLError.c][551] 
     Exit:[SQL_NO_DATA] 
[ODBC][14514][1373290079.706061][SQLFreeHandle.c][284] 
     Entry: 
      Handle Type = 2 
      Input Handle = 0x7fcbe1024800 
[ODBC][14514][1373290079.706195][SQLFreeHandle.c][333] 
     Exit:[SQL_SUCCESS] 
[ODBC][14514][1373290079.706415][SQLFreeHandle.c][219] 
     Entry: 
      Handle Type = 1 
      Input Handle = 0x7fcbe1004c00 

我使用山獅10 .8.4,unixodbc 2.3.1,freetds 0.91和SQL Server 2008.

謝謝!

回答

0

您可以通過指定ODBC的TDS版本設置

[MomImporterDSN] 
Description = Mom Importer 
Driver = /usr/local/Cellar/freetds/0.91/lib/libtdsodbc.so 
TDS_Version = 8.0 
Servername = db.mydomain.local 
Database = MomImporter 
UserName = mark.richman 
Password = mypassword 
+0

爲什麼我需要提供那些在〜/ .odbc.ini中指定的? –

+0

@MarkRichman你能看到我更新的答案 – meda

1

按照https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX

但是發出的所有指示修正此錯誤,還有一些額外的步驟和黑客如果ISQL/OSQL不工作(假設tsql工作正常)。他們聽起來很奇怪,但解決了這個問題(isql/osql沒有連接,雖然tsql工作正常)。

下面列出的每個步驟都是調試osql錯誤消息的結果。

在一個或多個步驟中,您可能需要額外的root權限。根據需要授予它們,並在達到pyodbc連接時撤銷。

  1. 的文件odbc.iniodbcinst.ini也應該出現在/Users/<username>/etc,除了官方的文檔指定位置。

  2. /(root directory)中也應該有odbcinst.ini的副本。

  3. 文件freetds.conf應存在於/usr/local/etc目錄中。

  4. 如果您使用SQL Server 2014及更高版本,則應將tds版本設置爲7.4。不要將tds版本設置爲8,因爲它只是7.1的別名。

  5. 檢查文件/usr/local/lib/libtdsodbc.so是否具有執行權限。如果不是,則通過chmod授予766/744/777(在最壞情況下)特權。這一步對於運行osql/isql和python的pyodbc連接是至關重要的。

在這些步驟之後,重新檢查一次的odbc.iniodbcinst.inifreetds.conf的內容,並確認它們是按照文檔(https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX

運行ISQL/OSQL。

如果一切正常,現在你已經設置好從python連接到SQLServer。以下片段顯示了基本的連接過程。

import pyodbc 

connection = pyodbc.connect('DSN=MYMSSQL;UID=<user_id for db_login>;PWD=<password_for db_login>) 
cursor = connection.cursor() 

result = cursor.execute("select @@VERSION").fetchall() 
print(result) 

cursor.close() 
connection.close()