userMod2
userMod2

Reputation: 8960

SQL Insert else Update not working

I'm trying to write a sql statement that does an insert if a certain row id doesn't exist else updates. So I've read and the below should work however it just freezes my SQLite database browser:

Any ideas?

INSERT INTO details
 (id, name, age)
VALUES
 (“1”, "mike", "22")
ON DUPLICATE KEY UPDATE
 name = “mike”
 age = “22”

Upvotes: 0

Views: 74

Answers (1)

Jeff Hawthorne
Jeff Hawthorne

Reputation: 568

it's hard to find current posts on the subject, but it appears that "ON DUPLICATE KEY UPDATE" does not exist in sqlite. you'll need to find an alternate way to do it. the two methods i've found are

1) ON CONFLICT...REPLACE http://www.sqlite.org/lang_conflict.html which doesn't update but deletes the given row then inserts it with the new values and

2) try/catch clause, something like this: http://blog.client9.com/2007/11/21/sqlite3-and-on-duplicate-key-update.html which is programmatically catching the error from the conflict, and enacting the update when the error occurs

Upvotes: 1

Related Questions