Re: How do I use parameterized queries with LIKE? - Mailing list psycopg

From Daniele Varrazzo
Subject Re: How do I use parameterized queries with LIKE?
Date
Msg-id CA+mi_8bD6PNf3CYDA3geq=gTT3JVbB7z7kK485QkYD_Bpi00QQ@mail.gmail.com
Whole thread Raw
In response to How do I use parameterized queries with LIKE?  ("W. Matthew Wilson" <matt@tplus1.com>)
Responses Re: How do I use parameterized queries with LIKE?  ("W. Matthew Wilson" <matt@tplus1.com>)
List psycopg
On Mon, May 28, 2012 at 11:00 PM, W. Matthew Wilson <matt@tplus1.com> wrote:

> But when I move the "matt" part out and use a %s symbol instead, I get
> this error:
>
>    ValueError: unsupported format character ''' (0x27) at index 73
>
> What is the right solution here?

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%',))

Hope this helps,

-- Daniele

psycopg by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: How do I use parameterized queries with LIKE?
Next
From: "P. Christeas"
Date:
Subject: Re: How do I use parameterized queries with LIKE?