pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: John Fabiani <johnf@jfcomputer.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: the use of $$string$$
Date: Mon, 07 Nov 2011 08:44:54 +0000
Message-ID: <4EB79A86.1090005@archonet.com> (raw)
In-Reply-To: <201111041712.09607.johnf@jfcomputer.com>
References: <201111040738.29303.johnf@jfcomputer.com>
	<201111040826.16177.johnf@jfcomputer.com>
	<4EB429AD.3030607@archonet.com>
	<201111041712.09607.johnf@jfcomputer.com>

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



view thread (11+ messages)  latest in thread

Message-ID: <4EB79A86.1090005@archonet.com>
Permalink:  ../4EB79A86.1090005@archonet.com/
Also on:    postgresql.org/message-id/4EB79A86.1090005@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, johnf@jfcomputer.com
  Subject: Re: the use of $$string$$
  In-Reply-To: <4EB79A86.1090005@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox