Message from discussion
I'm having trouble groking cursor.execute()
Received: by 10.224.188.65 with SMTP id cz1mr30826300qab.2.1327443315212;
Tue, 24 Jan 2012 14:15:15 -0800 (PST)
X-BeenThere: trac-dev@googlegroups.com
Received: by 10.224.172.152 with SMTP id l24ls2462260qaz.0.gmail; Tue, 24 Jan
2012 14:15:12 -0800 (PST)
Received: by 10.224.198.9 with SMTP id em9mr30799301qab.3.1327443312770;
Tue, 24 Jan 2012 14:15:12 -0800 (PST)
Received: by 10.224.198.9 with SMTP id em9mr30799297qab.3.1327443312756;
Tue, 24 Jan 2012 14:15:12 -0800 (PST)
Return-Path: <Chris.Nel...@sixnet.com>
Received: from mail.sixnet.com (static-64-22-42-34.albyny.csvoip.net. [64.22.42.34])
by gmr-mx.google.com with ESMTPS id jp9si11490715qcb.2.2012.01.24.14.15.12
(version=TLSv1/SSLv3 cipher=OTHER);
Tue, 24 Jan 2012 14:15:12 -0800 (PST)
Received-SPF: neutral (google.com: 64.22.42.34 is neither permitted nor denied by best guess record for domain of Chris.Nel...@sixnet.com) client-ip=64.22.42.34;
Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 64.22.42.34 is neither permitted nor denied by best guess record for domain of Chris.Nel...@sixnet.com) smtp.mail=Chris.Nel...@sixnet.com
Received: from [192.168.188.66] (192.168.188.66) by SVR26.sixnetio.com
(10.128.0.26) with Microsoft SMTP Server (TLS) id 14.1.339.1; Tue, 24 Jan
2012 17:15:11 -0500
Message-ID: <4F1F2D6F.4070108@SIXNET.com>
Date: Tue, 24 Jan 2012 17:15:11 -0500
From: Chris Nelson <Chris.Nel...@SIXNET.com>
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111108 Lightning/1.0b2 Thunderbird/3.1.16
MIME-Version: 1.0
To: <trac-dev@googlegroups.com>
Subject: Re: [Trac-dev] Re: I'm having trouble groking cursor.execute()
References: <4F1EF03E.7070802@SIXNET.com> <f15f481a-4353-422e-a68f-64349d9b06ff@q8g2000yqa.googlegroups.com>
In-Reply-To: <f15f481a-4353-422e-a68f-64349d9b06ff@q8g2000yqa.googlegroups.com>
Content-Type: text/plain; charset="ISO-8859-1"; format=flowed
Content-Transfer-Encoding: 7bit
Return-Path: Chris.Nel...@SIXNET.com
X-Originating-IP: [192.168.188.66]
On 01/24/2012 05:07 PM, osimons wrote:
>> ...
> However, what you are doing is mixing two concepts. The 'fixed'
> content of your SQL such as tables and column names cannot be replaced
> by dynamic variables, so for them you would need to do string
> interpolation so that your SQL is correct. As %s string interpolation
> will conflict with the argument substitution that comes later you need
> to escape the argument placeholder by using %%s (the first % escapes
> the second to treat it like a regular %).
>
> In python:
> >>> "SELECT * FROM %s WHERE id=%%s" % 'ticket'
> "SELECT * FROM ticket WHERE id=%s"
>
> So that means you want to combine the concepts:
> >>> cursor.execute("SELECT * FROM %s WHERE id=%%s" % 'ticket', [42])
Perfect. Thank you so much!
> It is all here in the docs:
>
> http://trac.edgewall.org/wiki/TracDev/DatabaseApi#RulesforDBAPIUsage
I'll look. Thanks.
--
Christopher Nelson, Software Engineering Manager
SIXNET - Solutions for Your Industrial Networking Challenges
331 Ushers Road, Ballston Lake, NY 12019
Tel: +1.518.877.5173, Fax: +1.518.877.8346 www.sixnet.com