Re: How do I use parameterized queries with LIKE?

From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: "W(dot) Matthew Wilson" <matt(at)tplus1(dot)com>
Cc: psycopg(at)postgresql(dot)org
Subject: Re: How do I use parameterized queries with LIKE?
Date: 2012-05-28 22:13:27
Message-ID: 4FC3F887.7010205@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On 05/28/2012 03:00 PM, W. Matthew Wilson wrote:
> This works just fine:
>
> cursor.execute("""select email_address from customer where
> email_address like '%matt%'""")
>
> 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?

http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries

So you need something like:

cursor.execute("""select email_address from customer where
email_address like %s""", ("matt",))

Note in particular the ("matt",). The parameters in this form need to be passed
as a tuple.

>
> Thanks for the help.
>
> Matt
>

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com

In response to

Browse psycopg by date

  From Date Subject
Next Message Daniele Varrazzo 2012-05-28 22:42:55 Re: How do I use parameterized queries with LIKE?
Previous Message W. Matthew Wilson 2012-05-28 22:00:03 How do I use parameterized queries with LIKE?