Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'programmer': 0.03; 'else:': 0.03; 'parameters': 0.04; 'none:': 0.07; 'subject:query': 0.07; 'string': 0.09; 'function,': 0.09; 'function:': 0.09; 'happens.': 0.09; 'none):': 0.09; 'parameter': 0.09; 'parsed': 0.09; 'part,': 0.09; 'sql,': 0.09; 'subject:()': 0.09; 'worse': 0.09; 'python': 0.11; 'def': 0.12; 'missed': 0.12; '2.7': 0.14; "%s'": 0.16; "(it's": 0.16; '42,': 0.16; "?',": 0.16; 'cleaner': 0.16; 'dict': 0.16; 'dictionary,': 0.16; 'hidden,': 0.16; 'manageable': 0.16; 'parameters,': 0.16; 'received:217.194': 0.16; 'subject:parameters': 0.16; 'substitution': 0.16; 'proprietary': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'skip:f 30': 0.19; 'help.': 0.21; 'seems': 0.21; '>>>': 0.22; 'code,': 0.22; 'memory': 0.22; 'header:User-Agent:1': 0.23; 'char': 0.24; 'format,': 0.24; 'parse': 0.24; 'received:192.168.1.100': 0.24; 'specify': 0.24; 'url:dev': 0.24; 'sort': 0.25; 'query': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; '>>>>': 0.31; 'enabled': 0.31; 'everywhere': 0.31; 'piece': 0.31; 'probably': 0.32; 'another': 0.32; 'text': 0.33; 'url:python': 0.33; 'plain': 0.33; 'skip:d 20': 0.34; 'problem': 0.35; 'something': 0.35; 'convert': 0.35; 'but': 0.35; 'really': 0.36; 'false': 0.36; 'thanks': 0.36; 'possible': 0.36; 'url:org': 0.36; 'received:it': 0.37; 'list': 0.37; 'performance': 0.37; 'thank': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'moving': 0.39; 'realize': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'even': 0.60; 'solve': 0.60; 'simply': 0.61; 'first': 0.61; 'received:217': 0.63; 'real': 0.63; 'become': 0.64; 'more': 0.64; 'avoid.': 0.84; 'id,': 0.84; 'much,': 0.84; 'otten': 0.84; 'subject:Using': 0.84; 'inefficient': 0.91; 'average': 0.93; 'catalog': 0.93 Date: Fri, 28 Mar 2014 15:16:05 +0100 From: Daniele Forghieri User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Using query parameters subtitution outside of execute() References: <5335367B.2090604@digitalfantasy.it> <533562E3.1000400@digitalfantasy.it> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 105 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396016174 news.xs4all.nl 2920 [2001:888:2000:d::a6]:43789 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69277 Il 28/03/2014 14:53, Peter Otten ha scritto: > 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) This one is, IMHO, cleaner and more manageable! Thank you very very much, I really appreciate your help. >> 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 ;) > > I'm trying but the old habits comes out, the first time I need to parse a string I do it like in C, looking at every single char using a couple of help function: the performance were really horrible, the memory used was huge than I change the way I do things (and start to use sqlite3 to store my data instead of using text files, parsed by a proprietary lib). The worse is the contrary, when I must use C and I think 'Here I use a dictionary, at the end convert it in a list that I sort with that key ...' only to realize that I don't have dictionary and the list I can use are very less manageable that the ones I used in Python ... It seems to me that going from C to Python you start writing inefficient code or write more lines than an average Python programmer but 'something moves' and the result happens. Moving from Python to C I always feel like I missed something and the first approach, good for Python, is simply not working in C ;( Thanks again Daniele Forghieri