Possible Duplicate:
SQLite - UPSERT not INSERT or REPLACE
How do I UPDATE a row in a table or INSERT it if it doesn't exist?
I'm developing metro app using Windows 8 release preview and C#(VS 2012), I'm new to SQLite, I integrated SQLite 3.7.13 in my App and it is working fine, In MYSQL we have like
INSERT INTO tablename (id, name)
VALUES (21, 'foo')
ON DUPLICATE KEY UPDATE name = 'boo';
How can we achieve this in SQLite 3 using Linq expression, Now I'm inserting the record and catching SQLiteException
and updating that record on exception.Please observe my code below
var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,"Student.db");
DateTime LastUpdated = DateTime.UtcNow;
using (var db = new SQLite.SQLiteConnection(dbPath))
{
try
{
db.Insert(new studentRecord() { sid = 21, name = 'foo', last_updated = LastUpdated });
}
catch(SQLite.SQLiteException ex)
{
db.Update(new studentRecord() { sid = 21, name = 'boo', last_updated = LastUpdated });
}
}
Where studentRecord
is my table name.
I'm not sure this is right way, Is there any way that we can achieve in a single query, Please help me. Thanks in advance