I am just starting to use wxPython with a relational database
(PostgreSQL), and am unsure of the best way to handle nulls when
inserting/updating. Some advice would be appreciated.
Imagine that txt_bar is an instance of wx.TextCtrl.
Here is an example code snippet:
···
-------------------------
bar = self.txt_bar.GetValue()
fred = ...
self.cur.execute("update foo set (bar, fred) = (%s, %s) where...")
-------------------------
(Note: I am putting the value into an intermediate variable so as to
avoid excessively long SQL statements)
If there is text in the control, all well and good. But if the user
elects to leave the control empty (which in this case is a valid option),
then the code as written will insert a zero-length string into the
database column. That is *not* what I am looking for; I want to put NULL
instead.
What I am unsure about is the most elegant way to achieve this. Is there
a way to do it without a series of "if" statements?