简体   繁体   English

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

[英]Connecting to MSSQL Server using pypyodbc on OS X

I'm having problems making a basic connection to a MSSQL Database Server using pypyodbc on OS X. 我在OS X上使用pypyodbc与MSSQL数据库服务器建立基本连接时遇到问题。

I have installed both unixodbc and freetds via Homebrew 我通过Homebrew安装了unixodbcfreetds

brew install unixodbc
brew install freetds

I then installed pypyodbc 然后我安装了pypyodbc

mkvirtualenv test
pip install pypyodbc

When I try to make a connection it fails with: 当我尝试建立连接时,它失败了:

$ 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")
>>>

My basic test.py looks like this: 我的基本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()

There are several similar questions but they don't' seem to be addressing the problem I'm having which seems to be related to a driver issue. 有几个类似的问题,但它们似乎并没有“解决我遇到的问题,这似乎与驱动程序问题有关。

$ 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
>>>

There seems to be a bug report reported here relating to this: https://code.google.com/p/pypyodbc/issues/detail?id=31 此处报告的错误报告似乎与此相关: https//code.google.com/p/pypyodbc/issues/detail?id = 31

I'm not sure if you solved your problem, but I just fought a similar issue and fixed it. 我不确定你是否解决了你的问题,但我只是打了一个类似的问题并修复了它。 I was getting the same "'utf32' codec can't decode bytes in position 0-1: truncated data" error. 我得到了相同的“'utf32'编解码器无法解码位置0-1中的字节:截断数据”错误。

What you need to double check is that in the odbc.ini file, the ServerName for the section is the same name in the freetds.conf file. 您需要仔细检查的是,在odbc.ini文件中,该部分的ServerName与freetds.conf文件中的名称相同。

For instance: 例如:

odbc.ini 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 freetds.conf

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

Anyway hope this helps. 无论如何希望这有帮助。

--------More info-------- You also need to make sure the DB user you created has proper access to the Database you are trying to read/write to. --------更多信息--------您还需要确保您创建的数据库用户可以正确访问您尝试读取/写入的数据库。 For me, I just needed read access. 对我来说,我只需要读访问权限。 You set this up in the Security node of the SQL Server Management Studio. 您可以在SQL Server Management Studio的安全性节点中进行设置。 Right click on the user then go to User Mapping. 右键单击用户,然后转到“用户映射”。 Check the appropriate databases, make sure to highlight the Database, then at the bottom set the database role. 检查相应的数据库,确保突出显示数据库,然后在底部设置数据库角色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM