Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #70752

Re: pyodbc connect string

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.071
X-Spam-Evidence '*H*': 0.86; '*S*': 0.00; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'accepting': 0.14; "'foobar'": 0.16; 'different,': 0.16; 'extraneous': 0.16; 'fails.': 0.16; 'finney': 0.16; 'mapping,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:" 70': 0.16; 'looked': 0.18; '>>>': 0.22; 'separate': 0.22; 'header:User-Agent:1': 0.23; 'example.': 0.24; 'specify': 0.24; 'string,': 0.24; 'header:X -Complaints-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'skip:p 30': 0.29; 'skip:( 20': 0.30; "skip:' 10": 0.31; 'keys': 0.31; 'larry': 0.31; 'occurs': 0.31; 'post.': 0.31; 'probability': 0.31; 'quotes': 0.31; 'writes:': 0.31; 'url:python': 0.33; 'problem': 0.35; 'but': 0.35; 'building': 0.35; 'there': 0.35; 'marks': 0.36; 'skip:" 50': 0.36; 'url:org': 0.36; 'ben': 0.38; 'to:addr:python- list': 0.38; 'fact': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'remove': 0.60; 'url:3': 0.61; 'simply': 0.61; 'first': 0.61; 'show': 0.63; 'more': 0.64; 'skip:\xe2 10': 0.65; 'world': 0.66; '8bit%:40': 0.68; 'improvements': 0.68; 'construction': 0.72; "'foo'": 0.84; 'fragments': 0.84; 'received:125': 0.84; 'url:reference': 0.84; 'yours': 0.88; 'not:': 0.91; '\xe2\x80\x9cthe': 0.91; 'quotation': 0.93; 'yours.': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben@benfinney.id.au>
Subject Re: pyodbc connect string
Date Wed, 30 Apr 2014 11:14:36 +1000
References <CACwCsY5VpHrO7-gpk88p+zPyX5VsVcZnv3q1u00F3DPJviaR3Q@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:7FFG20X4T4Vj1J1iFHUY03/im6k=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.9599.1398820493.18130.python-list@python.org> (permalink)
Lines 70
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1398820493 news.xs4all.nl 2888 [2001:888:2000:d::a6]:56544
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:70752

Show key headers only | View raw


Larry Martell <larry.martell@gmail.com> writes:

> I am having a problem building a connect string for pyodbc. It works
> when everything is hard coded, but if I build the connect string it
> fails.
>
> This works:
>
> pyodbc.connect('DRIVER=FreeTDS;' 'SERVER=xx.xx.xx.xx;' 'PORT=1433;'
> 'DATABASE=blah;' 'UID=foo;' 'PWD=bar;')

This calls the function with a single string,
"DRIVER=FreeTDS;SERVER=xx.xx.xx.xx;PORT=1433;DATABASE=blah;UID=foo;PWD=bar;".

Remember that consecutive, whitespace-separated, quote-delimited
fragments specify the construction of a single string literal::

    >>> 'foo'
    'foo'
    >>> 'foo' 'bar'
    'foobar'
    >>> 'foo' 'bar' 'baz'
    'foobarbaz'

See the reference for how this concatenation occurs
<URL:https://docs.python.org/3/reference/lexical_analysis.html#string-literal-concatenation>.

> But this does not:
>
> pyodbc.connect(conn_str)
>
> Where conn_str is:
>
> 'DRIVER=FreeTDS;' 'SERVER=xx.xx.xx.xx;' 'PORT=1433;' 'DATABASE=blah;'
> 'UID=foo;' 'PWD=bar;'

This string is different, because it contains a whole lot of quotation
marks and whitespace not in the string you show in the first example.

> conn_str is constructed with:
>
>  conn_str = "'DRIVER=%s;' 'SERVER=%s;' 'PORT=%s;' 'DATABASE=%s;'
> 'UID=%s;' 'PWD=%s;'" \
>                 % (RECIPE_DB['DRIVER'], RECIPE_DB['SERVER'],
> RECIPE_DB['PORT'], RECIPE_DB['DATABASE'],
>                    RECIPE_DB['USER'], RECIPE_DB['PASSWORD'])

Remove the extraneous quotes and whitespace, which were not in the
original string you showed above.

On a separate point: Since you have string keys for the mapping, you can
make the interpolation more readable::

    conn_str = (
            "DRIVER=%(DRIVER)s;SERVER=%(SERVER)s;PORT=%(PORT)s;"
            "DATABASE=%(DATABASE)s;UID=%(USER)s;PWD=%(PASSWORD)s;"
            ) % RECIPE_DB

since the named placeholders will be looked up by key in the ‘RECIPE_DB’
mapping.

There are other improvements to suggest, but I don't want to distract
from the main point of the post.

-- 
 \          “The fact that I have no remedy for all the sorrows of the |
  `\     world is no reason for my accepting yours. It simply supports |
_o__)  the strong probability that yours is a fake.” —Henry L. Mencken |
Ben Finney

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: pyodbc connect string Ben Finney <ben@benfinney.id.au> - 2014-04-30 11:14 +1000

csiph-web