I’m sure I can check if a row exists by selecting it but I’m wondering if there’s a slicker way that I’m just not aware of — seems like a common enough task that there might be. This SQLite table looks something like this:
rowID QID ANID value
------ ------ ----- ------
0 axo 1 45
1 axo 2 12
If the combination of QID and ANID already exists, that value should be updated, if the combination of QID and ANID doesn’t already exist then it should be inserted. While its simple enough to write:
SELECT * where QID = 'axo' and ANID = 3;
And check if the row exists then branch and either insert/update I can’t help but look for a better way. Thanks in advance!
The insert documentation details a REPLACE option
The “INSERT OR REPLACE” can abbreviated to REPLACE.
Example in Perl
Revised the following example to utilize a composite primary key
Produces the following output, demonstrating the update of one row and the insert of another