0

我已经使用 Homebrew 安装了 freetds。我的配置如下:

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

我正在使用 Mountain Lion 10.8.4、unixodbc 2.3.1、freetds 0.91 和 SQL Server 2008。

谢谢!

4

2 回答 2

2

按照https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX中给出的所有说明进行操作

但是,如果 isql/osql 不起作用(假设 tsql 工作正常),则几乎没有额外的步骤和技巧。它们可能听起来很奇怪,但为我解决了这个问题(虽然 tsql 工作正常,但 isql/osql 没有连接)。

下面列出的每个步骤都是调试 osql 错误消息的结果。

在一个或多个步骤中,您可能需要额外的 root 权限。根据需要授予它们,并在实现 pyodbc 连接后撤销它们。

  1. 除了官方文档指定的位置之外,这些文件odbc.iniodbcinst.ini应该存在于 中。/Users/<username>/etc

  2. odbcinst.iniin也应该有一个副本/(root directory)

  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()
于 2017-09-13T21:17:29.040 回答
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
于 2013-08-14T16:14:04.743 回答