Skip site navigation (1) Skip section navigation (2)

Re: How do I use parameterized queries with LIKE?

From: "W(dot) Matthew Wilson" <matt(at)tplus1(dot)com>
To: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
Cc: psycopg(at)postgresql(dot)org
Subject: Re: How do I use parameterized queries with LIKE?
Date: 2012-05-29 00:30:31
Message-ID: CAGHfCUDiK+MVfiYywe=_SeZk9FZPgCd2Ea+Mfyp7K7kBiWmw0g@mail.gmail.com (view raw or flat)
Thread:
Lists: psycopg
On Mon, May 28, 2012 at 6:42 PM, Daniele Varrazzo
<daniele(dot)varrazzo(at)gmail(dot)com> wrote:
> If you have parameters in the query, % is used as placeholder prefix.
> You must use %% to include a literal % in the query:
>
>    In [14]: cur.execute("""select email_address from customer where
>        email_address like '%%' || %s || '%%'""", ('matt',))
>
> or you can add the % to the value instead of the query:
>
>    In [17]: cur.execute("""select email_address from customer where
>        email_address like %s""", ('%matt%',))

Thanks so much!  I'm using the first solution, so that I can just
extract user-submitted data and pass it right in, rather than
prepending and appending '%' on the user data.

Matt



-- 
W. Matthew Wilson
matt(at)tplus1(dot)com
http://tplus1.com

In response to

Responses

psycopg by date

Next:From: Federico Di GregorioDate: 2012-05-29 06:28:58
Subject: Re: How do I use parameterized queries with LIKE?
Previous:From: P. ChristeasDate: 2012-05-28 22:44:26
Subject: Re: How do I use parameterized queries with LIKE?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group