Re: Error in PostgreSQL query with psycopg

From: "John D(dot) Burger" <john(at)mitre(dot)org>
To: PostgreSQL general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Error in PostgreSQL query with psycopg
Date: 2006-08-01 01:47:40
Message-ID: a4d948a5fe3c7f488512653811b4683a@mitre.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> never, never, never try quoting on your own! You can only fail. The
> only choice is to fail now or later.

Nonetheless, in case it's useful, here's a barebones wrapper I call on
everything:

def sqlValue (value):
if value is None:
return "NULL"
elif type(value) == types.StringType:
value = value.replace("'", "''")
# This is Postgres-specific - sigh
value = value.replace("\\", "\\\\")
return "'" + value + "'"
elif type(value) == types.UnicodeType:
return "'" + value.encode("UTF-8") + "'"
else:
return repr(value)

This has worked for me for several years, although I'm certain someone
will immediately poke holes in it. That's the other reason I posted
it!

- John Burger
MITRE

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Parthan SR 2006-08-01 04:22:24 Re: Error in PostgreSQL query with psycopg
Previous Message Simon Riggs 2006-08-01 00:53:34 Re: pg_xlog not cleaned up