sqlite3, qmarks, and NULL values

Marco Mariani marco at sferacarta.com
Wed May 20 09:56:59 EDT 2009


Mitchell L Model wrote:

>     def lookupxy(x, y):
>         if y:
>             conn.execute("SELECT * FROM table WHERE COL1 = ? AND COL2 = ?",
>                          (x, y))
>         else:
>             conn.execute("SELECT * FROM table WHERE COL1 = ? AND COL2 IS NULL",
>                          (x,))

> The more question marks involved the more complicated this would get, especially if question marks in the middle of several would sometimes need to be NULL. 


With SQLAlchemy you could write:

table.select().where((table.c.col1==x) & (table.c.col2==y))

where x or y are None, the sql engine generates the appropriate "IS 
NULL" clause.


> I hope I'm missing something and that someone can tell me what it is.


Yes, you are missing SQLAlchemy ;)



More information about the Python-list mailing list