Re: Idiot's query: proper way to handle NULL in SELECT query?
Jan UrbaĆski <[email protected]> Sun, 09 May 2010 02:35:20 +0200
| Newsgroups | gmane.comp.python.db.psycopg.devel |
|---|---|
| Message-ID | <[email protected]> |
On 09/05/10 01:46, Frank Miles wrote:
> As I have slowly been converting my system to use the adaptation
> methods, I've been using some direct conversions to handle NULL
> data. It would be nice to be able to:
>
> cur.execute("SELECT {variable-list} FROM table WHERE var1 = %s AND ... ;", (variable1,...))
>
> without having to pre-check each query, and alter it if one of the
How would you like it to behave in this case? You are correct, "variable
= NULL" is definitely not the same as "variable IS NULL" and psycopg2
cannot and will not assume what you had in mind when you wrote your
query code. It limits itself to converting your Python objects into
PostgreSQL literals.
> Confident that I have missed something blindingly obvious (yet haven't
> seen it in the documentation), I ask... what's the normal way of handlng
> this?
A wild guess: take a look at the COALESCE PostgreSQL function. But you
haven't really explained what is it that you would like psycopg2 to do,
so it's just a guess.
Cheers,
Jan