Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'parameters': 0.04; 'none:': 0.07; 'subject:query': 0.07; 'function,': 0.09; 'none):': 0.09; 'parameter': 0.09; 'part,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sql,': 0.09; 'subject:()': 0.09; 'python': 0.11; 'def': 0.12; '2.7': 0.14; "%s'": 0.16; "(it's": 0.16; '42,': 0.16; "?',": 0.16; 'dict': 0.16; 'hidden,': 0.16; 'parameters,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:parameters': 0.16; 'substitution': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'skip:f 30': 0.19; '>>>': 0.22; 'code,': 0.22; 'header:User-Agent:1': 0.23; 'format,': 0.24; 'specify': 0.24; 'url:dev': 0.24; 'query': 0.26; 'pass': 0.26; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'enabled': 0.31; 'everywhere': 0.31; 'piece': 0.31; 'probably': 0.32; 'another': 0.32; 'url:python': 0.33; 'plain': 0.33; 'skip:d 20': 0.34; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'false': 0.36; 'possible': 0.36; 'url:org': 0.36; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'solve': 0.60; 'real': 0.63; 'become': 0.64; 'avoid.': 0.84; 'id,': 0.84; 'otten': 0.84; 'subject:Using': 0.84; 'catalog': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Using query parameters subtitution outside of execute() Date: Fri, 28 Mar 2014 14:53:06 +0100 Organization: None References: <5335367B.2090604@digitalfantasy.it> <533562E3.1000400@digitalfantasy.it> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9041.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 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: 81 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396014805 news.xs4all.nl 2851 [2001:888:2000:d::a6]:37994 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69276 Daniele Forghieri wrote: > Il 28/03/2014 10:16, Peter Otten ha scritto: >> Daniele Forghieri wrote: >> >>> Hi to all. I'm using sqlite3 with python 2.7 on windows. >>> >>> I use the query substitution parameters in my query but I need to pass >>> part of the query to a function, something like (it's not the real >>> examples, just to clarify the question): >>> >>> def loadAll(cursor, id, queryAdd = None): >>> if queryAdd is None: >>> qry = 'select * from files where catalog = ?' >>> else: >>> qry = 'select * from files where catalog = ? and %s' % >>> (queryAdd)) >>> >>> cursor.execute(qry, (id, )) >>> ... >>> >>> I would like to use the query substitution even when I create, in >>> another piece of code, the queryAdd part, something like: >>> >>> queryAdd = cursor.querySubst('enabled = ? and hide = ? and data > ?', >>> (enabled, hidden, min_date, )) >>> >>> when the function take care of the date format, quoting the parameter >>> and so on >>> >>> It's possible or not ? >> You can use named parameters >> >> http://docs.python.org/dev/library/sqlite3.html#cursor-objects >> >> Your function might become (untested) >> >> def load_all(cursor, parameters, condition="catalog = :id"): >> query = 'select * from files where ' + condition >> cursor.execute(query, parameters) >> ... >> >> load_all( >> cursor, dict(id=42, fromdate=datetime.date.today()), >> condition="catalog = :id and date >= :fromdate") >> > > Thank. With this I can solve the problem but I have to specify the > query twice and if I have to change something I need to made it > everywhere I use the function and is something I would like to avoid. How about that one: def query_subst(sql, parameters): return sql, parameters def load_all(cursor, id, query_add=None): query = 'select * from files where catalog = ?' parameters = (id,) if query_add is not None: query += " and " + query_add[0] parameters += query_add[1] cursor.execute(query, parameters) ... enabled = True hidden = False min_date = datetime.date.today() query_add = query_subst( 'enabled = ? and hide = ? and date > ?', (enabled, hidden, min_date)) load_all(cs, 42, query_add) > I also don't like very mush to pass or create a dict for a function > call but that's probably me coming from old plain C ;) Get over it ;)