[Tutor] updating databases with null values

bob gailer bgailer at gmail.com
Thu Dec 9 18:34:15 CET 2010


On 12/9/2010 11:46 AM, Rance Hall wrote:
> I have a set of questions that ask about a customers name, address, email, etc.
>
> some of these values are allowed to be null, and others aren't.  Some
> are required to have specific formats when they aren't null.
>
> I'm happy with the code Ive written and its question asking routine,
> but I need help understanding how best to deal with null values.
>
> I'm connecting to a postgres database with the python postgres module
> described here: http://python.projects.postgresql.org/
>
> I'm also using python 3.x if it matters.
>
> The sql is different if you want to allow null values
>
> update table set value = "string" where condition
>
> with null value:
>
> update table set value = NULL where condition
>
> It would seem I need several iterations of the prepared db statement
> for various combinations of null values or I don't allow null values
> at all and put empty strings in the database instead of the null

Offhand I'd say you could use a parameterized stattement, like

update table set value = ? where condition

Then pass either the string or NULL when executing. I don't know 
postgresql's exact way to do this.

> value.
>
> Could someone help me think through this idea and help me decide on a
> good method.  Right now I'm thinking that I should skip null values
> and just store zero length strings in the db.  I don't like it, but
> its certainly much easier (at least to me so far)
>
> Your thoughts?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list