Re: the use of $$string$$

From: Richard Huxton <dev(at)archonet(dot)com>
To: John Fabiani <johnf(at)jfcomputer(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: the use of $$string$$
Date: 2011-11-07 08:44:54
Message-ID: 4EB79A86.1090005@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 05/11/11 00:12, John Fabiani wrote:
> I'm using psycopg2.

OK - bear in mind, I'm not a Python user.

> This is what I'm doing from python
> myvarString = "long string that contains single quotes"
> cusor.execute("insert into table (pkid, myfield) values (%s, $$%s$$)",(123,
> myvarString))
>
> When I execute the above I'm seeing:
> E'long string that contains single quotes' in the field. When I do a "select
> * from table" I get E'long string that contains single quotes'.

OK, so it seems psycopg is quoting your strings for you (as you'd
expect). It's presumably turning your query into:
... values (E'123', $$E'<long string>'$$)
So - the $$ quoting is unnecessary here - just use the % placeholders.

Incidentally, should it be %s for the numeric argument?

> myvarString = "long string that without single quotes"
> cusor.execute("insert into table (pkid, myfield) values (%s, %s)",(123,
> myvarString))
>
> I get the following:
> "long string that without single quotes"

That seems sensible to me (unless there's a typo in your example). You
shouldn't need to quote any of your values in your Python code - it's
doing it for you. I'm guessing there are other options beside %s for
other data-types (integers,floats,boolean etc).

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Olgierd Michalak 2011-11-07 19:25:07 Re: How to implement Aggregate Awareness?
Previous Message Manu T 2011-11-07 07:18:30 ORACLE PROCEDURE TO POSTGRES FUNCTION -ERROR IN THE OVER PART