gpt4 book ai didi

python - 如何使用 FreeTDS ODBC 连接到 SQL Server

转载 作者:行者123 更新时间:2023-12-05 00:47:24 27 4
gpt4 key购买 nike

我正在尝试通过我的 MacBook 连接到我公司的 SQL Server 数据库,并已按照此处列出的步骤进行操作:https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX但是当我进入以下步骤时,不断收到以下错误:

通过运行 isql TEST myuser mypassword 检查一切是否正常。您应该看到以下内容:

+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+

我已经验证了以下工作:

使用 tsql 实用程序测试连接,例如tsql -S TEST -U myuser -P mypassword。如果这有效,您应该会看到以下内容:

locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1>

odbcinst.ini 和 odbc.ini 配置文件都在同一个目录中。

MacBook-Pro: myname$ odbcinst -j
unixODBC 2.3.7
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /Users/myname/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

odbcinst.ini 文件配置:

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

odbc.ini 配置:

[TEST]
Description = Test to SQLServer
Driver = FreeTDS
Servername = ServerName

freetdf.conf

#   $Id: freetds.conf,v 1.12 2007-12-25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".

# Global settings are overridden by those in a database
# server specific section
[global]
# TDS protocol version
tds version = auto

# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff

# Command and connection timeouts
; timeout = 10
; connect timeout = 10

# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512

# If you experience TLS handshake errors and are using openssl,
# try adjusting the cipher list (don't surround in double or single quotes)
# openssl ciphers = HIGH:!SSLv2:!aNULL:-DH

# A typical Sybase server
[egServer50]
host = symachine.domain.com
port = 5000
tds version = 5.0

# A typical Microsoft server
[TEST]
host = ServerName
port = 1433
tds version = 7.3
client charset = UTF-8

我的命令和输出:isql TEST myuser mypass -v[IM002][unixODBC][Driver Manager]未找到数据源名称且未指定默认驱动程序[ISQL]错误:无法 SQLConnect

最佳答案

首先确认您正在编辑正确的配置文件。

您可以通过以下方式确认 FreeTDS 的世界观:

$ tsql -C
Compile-time settings (established with the "configure" script)
Version: freetds v1.1.11
freetds.conf directory: /usr/local/etc
MS db-lib source compatibility: no
Sybase binary compatibility: yes
Thread safety: yes
iconv library: yes
TDS version: 7.3
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: yes
OpenSSL: yes
GnuTLS: no
MARS: yes

这表明系统范围的 freetds.conf 文件将位于路径 /usr/local/etc/freetds.conf,尽管您可以拥有自己的~/.freetds.conf 中的用户特定版本。

如果您尝试连接到网络上的 SQL Server,fred.example.com,您可以在 freetds.conf 中为其创建别名。 :

[fred]
host = fred.example.com
port = 1433
tds version = auto
client charset = UTF-8

[fred] 别名不区分大小写。 TSQL 可以通过以下任何方式连接到它:

$ tsql -S fred -U "FRED\YourSQLUserName" -P "YourSQLPassword"
$ tsql -S FRED -U "FRED\YourSQLUserName" -P "YourSQLPassword"
$ tsql -S FrEd -U "FRED\YourSQLUserName" -P "YourSQLPassword"

...等等。

一旦您确定 FreeTDS 正在工作,您就可以继续使用 ODBC。您可以通过以下方式检查 ODBC 的世界观:

$ odbcinst -j
unixODBC 2.3.7
DRIVERS............: /usr/local/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: /Users/YourUserName/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

首先编辑 /usr/local/etc/odbcinst.ini 文件(一开始是空的)并添加以下内容:

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

接下来,您可以在 /usr/local/etc/odbc.ini 中为 Fred 添加系统范围的数据源,或者在 ~/.odbc.ini 中添加用户特定的数据源:

[fred]
Description = Test to SQLServer
Driver = FreeTDS
Servername = fred

注意这里的Servername = fred指向freetds.conf中的[fred]。它也不区分大小写,但您不应将一个称为 fred 而另一个称为 daphne

现在您应该能够使用 ODBC 连接:

$ isql fred "FRED\YourSQLUserName" "YourSQLPassword"

希望这会有所帮助。

关于python - 如何使用 FreeTDS ODBC 连接到 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57350910/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com