Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70754 > unrolled thread
| Started by | Larry Martell <larry.martell@gmail.com> |
|---|---|
| First post | 2014-04-29 19:25 -0600 |
| Last post | 2014-04-29 19:25 -0600 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: pyodbc connect string Larry Martell <larry.martell@gmail.com> - 2014-04-29 19:25 -0600
| From | Larry Martell <larry.martell@gmail.com> |
|---|---|
| Date | 2014-04-29 19:25 -0600 |
| Subject | Re: pyodbc connect string |
| Message-ID | <mailman.9601.1398821153.18130.python-list@python.org> |
On Tue, Apr 29, 2014 at 7:14 PM, Ben Finney <ben@benfinney.id.au> wrote:
> 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.
Thanks for the explanation.
Back to top | Article view | comp.lang.python
csiph-web