Eric Minbiole wrote:
>> I have a table like this
>>
>> CREATE TABLE foo (bar TEXT NOT NULL DEFAULT 'default_value');
>>
>> and I'd like to create a reusable statement to do inserts into foo, like 
>> this:
>>
>> INSERT INTO foo (bar) VALUES (?);
>>
>> Sometimes I have values for bar and sometimes I don't and want the
>> default. Is there any way to indicate to the statement that I want the
>> bound parameter to be "nothing" therefore giving me the default value?
>> If I bind that column to NULL I get a constraint error.
> 
> Could you simply create two separate prepared statements (one which sets 
> bar and one which doesn't), then use the appropriate one, depending on 
> whether you know the value of bar?  Perhaps not as elegant as reusing 
> one statement for everything, but it should work easily enough.
> 

Eric's suggestion is the only correct way to do this. The default value 
is only used if no value, not even a null, is supplied by the insert 
statement. You need one statement that supplies a bar value, and one 
that does not, which will use the default value.

If you have lots of fields that you want to do this with the required 
number of combinations can get large very quickly, so you may be better 
off building the statements on the fly.

HTH
Dennis Cote
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to