[DB-SIG] PyGreSQL and NULLs

Denis S. Otkidach ods at strana.ru
Tue Feb 10 13:14:54 EST 2004


On Tue, 10 Feb 2004, Andy Todd wrote:

AT> > my_user = {"id":10,"name":"Marc"}
AT> >
AT> > cursor.execute("INSERT into myTable (my_id, my_name,
AT> my_pet_id) values " +
AT> >             "(%(id)s,'%(name)s',%(pet)s) % my_user)
AT> >
AT> > would result in this SQL command,
AT> >
AT> > INSERT into myTable (my_id, my_name, my_pet_id) values
AT> (10, 'Marc', );
[...]
AT>
AT> I don't have Postgres or PyGreSQL installed but I suspect
AT> that if you
AT> change the first line to;
AT>
AT> my_user = {'id':10, 'name':'Marc', 'pet':None}
AT>
AT> You would get the result you are expecting.

No, Python formating operator know nothing about DB API and
cannot transfrom None to 'NULL'.  There are several
errors in original sample:
- no quote at the and of format string (leads to SyntaxError);
- no value with key 'pet' (leads to KeyError);
- Python formating operator % is used instead of passing
parameters as second argument of execute.

-- 
Denis S. Otkidach
http://www.python.ru/      [ru]




More information about the DB-SIG mailing list