Re: Error in PostgreSQL query with psycopg - Mailing list pgsql-general

From John D. Burger
Subject Re: Error in PostgreSQL query with psycopg
Date
Msg-id a4d948a5fe3c7f488512653811b4683a@mitre.org
Whole thread Raw
In response to Re: Error in PostgreSQL query with psycopg  ("Harald Armin Massa" <haraldarminmassa@gmail.com>)
Responses Re: Error in PostgreSQL query with psycopg  (Martijn van Oosterhout <kleptog@svana.org>)
List 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


pgsql-general by date:

Previous
From: Simon Riggs
Date:
Subject: Re: pg_xlog not cleaned up
Next
From: Christian Kastner
Date:
Subject: Re: pg_xlog not cleaned up