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

--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Reply via email to