Documentation error in mssql+pymssql connection string

Issue #3696 resolved
Rudolf Cardinal created an issue

The docs at http://docs.sqlalchemy.org/en/latest/dialects/mssql.html#module-sqlalchemy.dialects.mssql.pymssql give the following prototype connection string for SQL Server (mssql) via pymssql:

mssql+pymssql://<username>:<password>@<freetds_name>?charset=utf8

However (with SQL Alchemy 1.0.12 via "pip install"), this fails. Traceback below. But the basic reason appears to be that the "?charset=utf8" gets incorporated into the "host" parameter.

The URL used was:

mssql+pymssql://researcher:blibble@crate_sqlserver_test?charset=utf8

where crate_sqlserver_test is defined in /etc/freetds/freetds.conf. Inserting a debugging print statement into DefaultDialect.connect(), in sqlalchemy/engine/default.py, where it calls

return self.dbapi.connect(*cargs, **cparams)

gives the following:

cargs: ()
cparams: {'password': 'blibble', 'host': 'crate_sqlserver_test?charset=utf8', 'user': 'researcher'}

(note the error for host).

Traceback (most recent call last):
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/pool.py", line 1044, in _do_get
    return self._pool.get(wait, self._timeout)
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/util/queue.py", line 145, in get
    raise Empty
sqlalchemy.util.queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "pymssql.pyx", line 635, in pymssql.connect (pymssql.c:10699)
  File "_mssql.pyx", line 1900, in _mssql.connect (_mssql.c:21951)
  File "_mssql.pyx", line 636, in _mssql.MSSQLConnection.__init__ (_mssql.c:6545)
_mssql.MSSQLDriverException: Connection to the database failed for an unknown reason.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/engine/base.py", line 2074, in _wrap_pool_connect
    return fn()
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/pool.py", line 318, in unique_connection
    return _ConnectionFairy._checkout(self)
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/pool.py", line 713, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/pool.py", line 480, in checkout
    rec = pool._do_get()
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/pool.py", line 1060, in _do_get
    self._dec_overflow()
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/util/compat.py", line 184, in reraise
    raise value
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/pool.py", line 1057, in _do_get
    return self._create_connection()
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/pool.py", line 323, in _create_connection
    return _ConnectionRecord(self)
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/pool.py", line 449, in __init__
    self.connection = self.__connect()
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/pool.py", line 607, in __connect
    connection = self.__pool._invoke_creator(self)
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/engine/strategies.py", line 97, in connect
    return dialect.connect(*cargs, **cparams)
  File "/home/rudolf/venvs/crate/lib/python3.4/site-packages/sqlalchemy/engine/default.py", line 387, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "pymssql.pyx", line 644, in pymssql.connect (pymssql.c:10856)
pymssql.InterfaceError: Connection to the database failed for an unknown reason.

Comments (4)

  1. Mike Bayer repo owner

    OK well I didn't see a suggestion for the fix here, however this works:

    mssql+pymssql://scott:tiger@192.168.122.232:1213/?charset=utf8
    

    e.g. missing a slash. Can you confirm that works for you? (feel free to provide a PR)

  2. Rudolf Cardinal reporter

    Thank you; yes, that works fine. Apologies; I am a novice and unsure how to create a pull request or change the documentation, but the only place I've found a reference is in lib/sqlalchemy/dialects/mssql/pymssql.py, for which the diff is:

    @@ -9,7 +9,7 @@
     .. dialect:: mssql+pymssql
         :name: pymssql
         :dbapi: pymssql
    -    :connectstring: mssql+pymssql://<username>:<password>@<freetds_name>?\
    +    :connectstring: mssql+pymssql://<username>:<password>@<freetds_name>/?\
     charset=utf8
         :url: http://pymssql.org/
    
  3. Log in to comment