title: Connecting to MSSQL Server using pypyodbc on OS X
body: <p>I'm having problems making a basic connection to a MSSQL Database Server using <a href="https://pypi.python.org/pypi/pypyodbc" rel="nofollow">pypyodbc</a> on OS X.</p>
  
  <p>I have installed both <code>unixodbc</code> and <code>freetds</code> via <a href="https://brew.sh" rel="nofollow">Homebrew</a></p>
  
  <pre><code>brew install unixodbc
  brew install freetds
  </code></pre>
  
  <p>I then installed <code>pypyodbc</code></p>
  
  <pre><code>mkvirtualenv test
  pip install pypyodbc
  </code></pre>
  
  <p>When I try to make a connection it fails with:</p>
  
  <pre><code>$ python -i test.py
  Traceback (most recent call last):
    File "test.py", line 20, in &lt;module&gt;
      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")
  &gt;&gt;&gt;
  </code></pre>
  
  <p>My basic <code>test.py</code> looks like this:</p>
  
  <pre><code>#!/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()
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>$ python -i test.py
  Username: IRMA_RO
  Password:
  Traceback (most recent call last):
    File "test.py", line 15, in &lt;module&gt;
      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
  &gt;&gt;&gt;
  </code></pre>
  
  <p>There seems to be a bug report reported here relating to this: <a href="https://code.google.com/p/pypyodbc/issues/detail?id=31" rel="nofollow">https://code.google.com/p/pypyodbc/issues/detail?id=31</a></p>