summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2019-10-31 10:30:46 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2019-10-31 10:37:27 -0400
commit9cf02d6703f7d3084e40a7248023f10ed20a4724 (patch)
treeb8776ba3d6ee59633224b2613862e8d40810e203
parentad0bece8645612335a4acb54b2097debde8dce1d (diff)
downloadsqlalchemy-9cf02d6703f7d3084e40a7248023f10ed20a4724.tar.gz
Improve SQL Server pyodbc documentation
While we were told years ago that ODBC is intended to be used with DSNs only, however this use does not correspond well with how most other database connectivity systems work in that modern systems already have their own registries of connection information in any case, meaning this is usually the best place to add details such as hostnames and driver names, rather than having them locked away in a server-specific ODBC registry. So here we dial back the language that one style or another of connecting is "preferred"; both styles are supported equally, and the critical advantage of hostname mapping in that the target database name is both explicit as well as modifyable is also added. Add additional background for how DSNs work and refine other sentences. "URL encoding" is the correct terminology for adding spaces and special characters to a URL. Change-Id: I13a74432976e6d3166633b98f9bb84c4856caac8 (cherry picked from commit 65466dec6346ad84340af1cf3e431020add0f9a5) (cherry picked from commit e2dc03ff0983a36ce132733b6a1f82ec3ee344da)
-rw-r--r--lib/sqlalchemy/dialects/mssql/pyodbc.py31
1 files changed, 18 insertions, 13 deletions
diff --git a/lib/sqlalchemy/dialects/mssql/pyodbc.py b/lib/sqlalchemy/dialects/mssql/pyodbc.py
index 98b7cad32..3e9b14070 100644
--- a/lib/sqlalchemy/dialects/mssql/pyodbc.py
+++ b/lib/sqlalchemy/dialects/mssql/pyodbc.py
@@ -21,8 +21,11 @@ detailed in `ConnectionStrings <https://code.google.com/p/pyodbc/wiki/Connection
DSN Connections
^^^^^^^^^^^^^^^
-A DSN-based connection is **preferred** overall when using ODBC. A
-basic DSN-based connection looks like::
+A DSN connection in ODBC means that a pre-existing ODBC datasource is
+configured on the client machine. The application then specifies the name
+of this datasource, which encompasses details such as the specific ODBC driver
+in use as well as the network address of the database. Assuming a datasource
+is configured on the client, a basic DSN-based connection looks like::
engine = create_engine("mssql+pyodbc://scott:tiger@some_dsn")
@@ -36,15 +39,16 @@ the ``Trusted_Connection=yes`` directive to the ODBC string.
Hostname Connections
^^^^^^^^^^^^^^^^^^^^
-Hostname-based connections are **not preferred**, however are supported.
-The ODBC driver name must be explicitly specified::
+Hostname-based connections are also supported by pyodbc. These are often
+easier to use than a DSN and have the additional advantage that the specific
+database name to connect towards may be specified locally in the URL, rather
+than it being fixed as part of a datasource configuration.
- engine = create_engine("mssql+pyodbc://scott:tiger@myhost:port/databasename?driver=SQL+Server+Native+Client+10.0")
+When using a hostname connection, the driver name must also be specified in the
+query parameters of the URL. As these names usually have spaces in them, the
+name must be URL encoded which means using plus signs for spaces::
-.. versionchanged:: 1.0.0 Hostname-based PyODBC connections now require the
- SQL Server driver name specified explicitly. SQLAlchemy cannot
- choose an optimal default here as it varies based on platform
- and installed drivers.
+ engine = create_engine("mssql+pyodbc://scott:tiger@myhost:port/databasename?driver=SQL+Server+Native+Client+10.0")
Other keywords interpreted by the Pyodbc dialect to be passed to
``pyodbc.connect()`` in both the DSN and hostname cases include:
@@ -53,10 +57,11 @@ Other keywords interpreted by the Pyodbc dialect to be passed to
Pass through exact Pyodbc string
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-A PyODBC connection string can also be sent exactly as specified in
-`ConnectionStrings <https://code.google.com/p/pyodbc/wiki/ConnectionStrings>`_
-into the driver using the parameter ``odbc_connect``. The delimeters must be URL escaped, however,
-as illustrated below using ``urllib.quote_plus``::
+A PyODBC connection string can also be sent in pyodbc's format directly, as
+specified in `ConnectionStrings
+<https://code.google.com/p/pyodbc/wiki/ConnectionStrings>`_ into the driver
+using the parameter ``odbc_connect``. The delimeters must be URL encoded, as
+illustrated below using ``urllib.parse.quote_plus``::
import urllib
params = urllib.quote_plus("DRIVER={SQL Server Native Client 10.0};SERVER=dagger;DATABASE=test;UID=user;PWD=password")