Re: auto escaping "NULL"
Jacob Joseph <[email protected]>
| Newsgroups | gmane.comp.python.db.psycopg.devel |
|---|---|
| Message-ID | <[email protected]> |
On Fri, 04 Dec 2009 09:56:51 +0100 Federico Di Gregorio <fog-NGVKUo/i/[email protected]> wrote: > Il giorno gio, 03/12/2009 alle 18.24 -0500, Jacob Joseph ha scritto: > > Hi. > > Upon trying to insert NULL values into a double precision field, I > > receive the error: > > > > DataError: invalid input syntax for type double precision: "NULL" > > LINE 4: (2, -1, E'NULL', 1.11, > > > > I'm using a statement like: > > curs.execute("INSERT INTO foo (distance) VALUES (%(dist)s)", > > {'dist': None}) > > That's the correct way and should work. The code you show us is not > the code you're actually executing? ;) Thank you all for your responses. It's what I *thought* I was executing, but some older code had helpfully converted the None to a 'NULL' string, already. > > As a related question, what is the correct way to use the execute(s, > > args) syntax, but *not* escape a particular variable? Not so > > infrequently, I'd like to use an argument to add an SQL command, > > such as adding to a WHERE or specifying a field. > > You should use the normal % operator to build a query string and then > pass it to execute. If you really want to avoid quoting of a bound > variable you can import psycopg2.extras and then wrap the variable in > AsIs() (AsIs just call wrapped object's str() method): > > curs.execute("INSERT INTO foo (distance) VALUES (%(dist)s)", > {'dist': AsIs("NULL")}) > > But note that this example is a bad use case bacause psycopg knows how > to parse None and you should use AsIs only in very special cases (and > then be ashamed of it.) Thanks. I'll use it with trepidation. ~Jacob _______________________________________________ Psycopg mailing list Psycopg-IAPFreCvJWPBWskQ1e/[email protected] http://lists.initd.org/mailman/listinfo/psycopg
signature.asc
(application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQFLGVuPMpwSl5zyBnARAsOoAJ40UOrTlOMXJ+LY4D0Cw0eDVqSlLwCghJhL dfVC11UvEjzs0KcMorWoVMw= =17oj -----END PGP SIGNATURE-----