gpt4 book ai didi

python - 在 OS X 上使用 pypyodbc 连接到 MSSQL Server

转载 作者:行者123 更新时间:2023-11-28 16:37:07 25 4
gpt4 key购买 nike

我在使用 pypyodbc 与 MSSQL 数据库服务器建立基本连接时遇到问题在 OS X 上。

我已经通过 Homebrew 安装了 unixodbcfreetds

brew install unixodbc
brew install freetds

然后我安装了pypyodbc

mkvirtualenv test
pip install pypyodbc

当我尝试建立连接时,它失败了:

$ python -i test.py
Traceback (most recent call last):
File "test.py", line 20, in <module>
c = p.connect(dsn)
File "/Users/xxxxxxxx/.virtualenvs/symplectic_cleanup/lib/python2.7/site-packages/pypyodbc.py", line 2434, in __init__
self.connect(connectString, autocommit, ansi, timeout, unicode_results, readonly)
File "/Users/xxxxxxxx/.virtualenvs/symplectic_cleanup/lib/python2.7/site-packages/pypyodbc.py", line 2483, in connect
check_success(self, ret)
File "/Users/xxxxxxxx/.virtualenvs/symplectic_cleanup/lib/python2.7/site-packages/pypyodbc.py", line 988, in check_success
ctrl_err(SQL_HANDLE_DBC, ODBC_obj.dbc_h, ret, ODBC_obj.ansi)
File "/Users/xxxxxxxx/.virtualenvs/symplectic_cleanup/lib/python2.7/site-packages/pypyodbc.py", line 966, in ctrl_err
raise DatabaseError(state,err_text)
pypyodbc.DatabaseError: (u'01000', u"[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found")
>>>

我的基本 test.py 如下所示:

#!/usr/bin/env python
# -*- coding: utf-8 -*-


import pypyodbc as p


settings = {
"driver": "FreeTDS",
"hostname": "mssql.local",
"username": "testuser",
"password": "testpass",
"database": "testdb",
"port": 1433
}


dsn = "DRIVER={{{driver:s}}};SERVER={hostname:s};PORT={port:d};DATABASE={database:s};UID={username:s};PWD={password:s};CHARSET=UTF8;TDS_Version=8.0".format(**settings) # noqa

c = p.connect(dsn)

xs = c.execute("SELECT name FROM master..sysdatabases").fetchall()

有几个类似的问题,但它们似乎没有解决我遇到的似乎与驱动程序问题有关的问题。

$ python -i test.py
Username: IRMA_RO
Password:
Traceback (most recent call last):
File "test.py", line 15, in <module>
c = p.connect("DSN=na-dev;UID={0:s};PWD={1:s}".format(username, password))
File "/Users/xxxxxxxx/.virtualenvs/symplectic_cleanup/lib/python2.7/site-packages/pypyodbc.py", line 2434, in __init__
self.connect(connectString, autocommit, ansi, timeout, unicode_results, readonly)
File "/Users/xxxxxxxx/.virtualenvs/symplectic_cleanup/lib/python2.7/site-packages/pypyodbc.py", line 2483, in connect
check_success(self, ret)
File "/Users/xxxxxxxx/.virtualenvs/symplectic_cleanup/lib/python2.7/site-packages/pypyodbc.py", line 988, in check_success
ctrl_err(SQL_HANDLE_DBC, ODBC_obj.dbc_h, ret, ODBC_obj.ansi)
File "/Users/xxxxxxxx/.virtualenvs/symplectic_cleanup/lib/python2.7/site-packages/pypyodbc.py", line 975, in ctrl_err
err_list.append((from_buffer_u(state), from_buffer_u(Message), NativeError.value))
File "/Users/xxxxxxxx/.virtualenvs/symplectic_cleanup/lib/python2.7/site-packages/pypyodbc.py", line 482, in UCS_dec
uchar = buffer.raw[i:i + ucs_length].decode(odbc_decoding)
File "/Users/xxxxxxxx/.virtualenvs/symplectic_cleanup/lib/python2.7/encodings/utf_32.py", line 11, in decode
return codecs.utf_32_decode(input, errors, True)
UnicodeDecodeError: 'utf32' codec can't decode bytes in position 0-1: truncated data
>>>

这里似乎有一个与此相关的错误报告:https://code.google.com/p/pypyodbc/issues/detail?id=31

最佳答案

我不确定你是否解决了你的问题,但我刚刚解决了一个类似的问题并解决了它。我遇到了相同的“'utf32' 编解码器无法解码位置 0-1 中的字节:截断数据”错误。

您需要仔细检查的是,在 odbc.ini 文件中,该部分的 ServerName 与 freetds.conf 文件中的名称相同。

例如:

odbc.ini

[old_flood]
Driver = FreeTDS
Description = RTS Old Flood
ServerName = old_flood <----- this must be the same as the name in freetds.conf

freetds.conf

[old_flood]                  <----- This is the same as the ServerName above
host = <server ip address>
port = <server port>
tds version = 8.0

无论如何希望这对您有所帮助。

--------更多信息--------您还需要确保您创建的数据库用户对您尝试读/写的数据库具有适当的访问权限。对我来说,我只需要读取权限。您在 SQL Server Management Studio 的安全节点中设置它。右键单击用户,然后转到用户映射。检查适当的数据库,确保突出显示数据库,然后在底部设置数据库角色。

关于python - 在 OS X 上使用 pypyodbc 连接到 MSSQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24521679/

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