Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.074 X-Spam-Evidence: '*H*': 0.85; '*S*': 0.00; 'string': 0.09; 'subject:string': 0.09; 'cc:addr:python-list': 0.11; "'foobar'": 0.16; 'different,': 0.16; 'extraneous': 0.16; 'fails.': 0.16; 'finney': 0.16; 'mapping,': 0.16; 'skip:" 70': 0.16; 'wrote:': 0.18; 'looked': 0.18; '>>>': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'example.': 0.24; 'specify': 0.24; 'string,': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'skip:p 30': 0.29; 'message- id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "skip:' 10": 0.31; 'keys': 0.31; 'larry': 0.31; 'occurs': 0.31; 'post.': 0.31; 'quotes': 0.31; 'writes:': 0.31; 'url:python': 0.33; 'problem': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'building': 0.35; 'there': 0.35; 'marks': 0.36; 'skip:" 50': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'ben': 0.38; 'pm,': 0.38; 'does': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'remove': 0.60; 'url:3': 0.61; 'first': 0.61; 'show': 0.63; 'more': 0.64; 'skip:\xe2 10': 0.65; '8bit%:40': 0.68; 'improvements': 0.68; 'construction': 0.72; "'foo'": 0.84; 'fragments': 0.84; 'url:reference': 0.84; 'not:': 0.91; 'quotation': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=u0dJNSPcEqIBXCZ2TzpGpCUgacRTlf/IPqpHScLKPzM=; b=BFVvGt6+cnS+eJTw00cpwxfHqueXbTZRhvLMzObAXK+Qv+Anit+7v3wXM4Uqc93xf6 My16jqQphHGlPbOUjfcA/VCYWU31GNyPoKi5yYdW2vDD0scXpNk0Mnb4+HEo32VheqPn rxEUQwpbZ5vX5Jgq7CFK+QEpzQ1qy+AhF3LDJjtycj9u92Ykk7VeeoCwnENbRdkTmXJG vpN6kWrWlzoxfOoec7t6J5uPebqo5yjzk/q7uGZtEyfKFBU2MuIWVfPAAeYNbg8z3H8O lqMq2g9GiVocyOvcK3zR++ykySP0u8S13g8brMH1nkdaKeLeckymDpdaZNfl7dhfiQLr E54A== MIME-Version: 1.0 X-Received: by 10.180.89.1 with SMTP id bk1mr915191wib.35.1398821151056; Tue, 29 Apr 2014 18:25:51 -0700 (PDT) In-Reply-To: <85wqe7ipoj.fsf@benfinney.id.au> References: <85wqe7ipoj.fsf@benfinney.id.au> Date: Tue, 29 Apr 2014 19:25:51 -0600 Subject: Re: pyodbc connect string From: Larry Martell To: Ben Finney Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1398821153 news.xs4all.nl 2839 [2001:888:2000:d::a6]:33856 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70754 On Tue, Apr 29, 2014 at 7:14 PM, Ben Finney wrote: > Larry Martell 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=3DFreeTDS;' 'SERVER=3Dxx.xx.xx.xx;' 'PORT=3D1433;= ' >> 'DATABASE=3Dblah;' 'UID=3Dfoo;' 'PWD=3Dbar;') > > This calls the function with a single string, > "DRIVER=3DFreeTDS;SERVER=3Dxx.xx.xx.xx;PORT=3D1433;DATABASE=3Dblah;UID=3D= foo;PWD=3Dbar;". > > 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 > . > >> But this does not: >> >> pyodbc.connect(conn_str) >> >> Where conn_str is: >> >> 'DRIVER=3DFreeTDS;' 'SERVER=3Dxx.xx.xx.xx;' 'PORT=3D1433;' 'DATABASE=3Db= lah;' >> 'UID=3Dfoo;' 'PWD=3Dbar;' > > 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 =3D "'DRIVER=3D%s;' 'SERVER=3D%s;' 'PORT=3D%s;' 'DATABASE=3D%s= ;' >> 'UID=3D%s;' 'PWD=3D%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 =3D ( > "DRIVER=3D%(DRIVER)s;SERVER=3D%(SERVER)s;PORT=3D%(PORT)s;" > "DATABASE=3D%(DATABASE)s;UID=3D%(USER)s;PWD=3D%(PASSWORD)s;" > ) % RECIPE_DB > > since the named placeholders will be looked up by key in the =E2=80=98REC= IPE_DB=E2=80=99 > 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.