Re: [sqlite] INSERTing a row only if it doesn't EXIST

2005-09-17 Thread Puneet Kishor


On Sep 17, 2005, at 4:35 PM, Alexander J. Kozlovsky wrote:


I want to avoid doing a two step process outside the db... I want to
insert a row only if it doesn't exist already.


IMHO, if you table T1 have a unique key, you may do this

INSERT OR IGNORE T1 VALUES(1, 2, 3);




ahhh! the conflict algorithm. Thanks for drawing my attention to it... 
it should do the trick.



--
Puneet Kishor



Re: [sqlite] INSERTing a row only if it doesn't EXIST

2005-09-17 Thread Kurt Welgehausen
If id in your example identifies a row, then by
definition it is unique (probably the primary
key). If you try to insert another row with the
same id, the insert will fail. Why not just
catch the exception or error code?

Regards


Re: [sqlite] INSERTing a row only if it doesn't EXIST

2005-09-17 Thread Alexander J. Kozlovsky
> I want to avoid doing a two step process outside the db... I want to
> insert a row only if it doesn't exist already.

IMHO, if you table T1 have a unique key, you may do this

INSERT OR IGNORE T1 VALUES(1, 2, 3);


Best regards,
 Alexandermailto:[EMAIL PROTECTED]



[sqlite] INSERTing a row only if it doesn't EXIST

2005-09-17 Thread Puneet Kishor
I want to avoid doing a two step process outside the db... I want to 
insert a row only if it doesn't exist already. REPLACE INTO seems to 
almost do the trick, however, seems like it will UPDATE if the row 
already exists. I want the row to be left alone if it exists.


Am trying to figure out if EXISTS could be creatively used to do this

SELECT EXISTS (SELECT id FROM t WHERE id = 5)

return 1 if 5 exists, and 0 if it doesn't exist. Is it possible to do 
an INSERT based on this? Or, do I have to use the logic in my 
application? My brain is not working, so any help on this would be 
appreciated.



--
Puneet Kishor