【发布时间】:2014-07-02 01:44:26
【问题描述】:
我在 OS X 上使用 pypyodbc 与 MSSQL 数据库服务器建立基本连接时遇到问题。
我已经通过Homebrew 安装了unixodbc 和freetds
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
【问题讨论】:
-
虽然这可能无法用
pyodbc或pypyodbc(纯Python) 解决这个问题中的问题,但我找到了另一个似乎无法解决的解决方案-stackoverflow.com/questions/11678696/sql-server-python-and-os-x 给我的盒子,使用 pymssql 和教程:pymssql.sourceforge.net/examples_pymssql.php
标签: python sql-server macos odbc pypyodbc