开发者

postgresql handling null values

I am using postgresql for database. And the condition on the field is not null. When user keeps blank for that field and hit on enter its giving null pointer exception becuase in postgresq开发者_开发百科l for integer it wont accept accept "".Any idea about how to handle this case


If you can modify the INSERT statement, you can use the COALESCE() function to automatically replace a NULL value with another value you specify.

Here is an example to automatically insert 0 (zero) in the status field, when the $status variable is not set:

INSERT INTO foo (name, status) VALUES ($name, COALESCE($status, 0) );

The COALESCE function can take any number of arguments and returns the first non-NULL value it finds. This comes in handy when you have a "waterfall" of values you want to try before settling on the default value. For example:

COALESCE($status1, $status2, $status3, $status4, 0)


Since your field does not accept NULL, you should set it with an actual integer.

Replace the empty string with a 0 (or other default value) on the client side.


Check your form with javascript, and if is coming a "" from the textbox, just assign 0 to that value. Then pass that value as an Integer value and that's all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜