Hello Friends 👋,
Welcome To Infinitbility! ❤️
For most of Crud and migration query time, we try to make the same query for insert or update like if we have the same row then update else insert a new row.
SQLite also provides multiple options to write insert or update statements in a single query.
Let start today’s tutorial SQLite insert or update statement example
First we should go with new SQLite feature SQLite upserts
In SQLite upserts, we are able to write conditions if column values match then run update query else insert.
Something like the below example.
1INSERT INTO phonebook(name,phonenumber) VALUES('Alice','704-555-1212')2 ON CONFLICT(name) DO UPDATE SET phonenumber=excluded.phonenumber;
One thing I don’t like, we have to write columns multiple times.
We have also a second option and it’s the
INSERT OR REPLACE query.
SQLite INSERT OR REPLACE
SQLite REPLACE will delete the existing one and add a new row in the table then use this very carefully and provide all column details while in updating case else other data will vanish.
1INSERT OR REPLACE INTO phonebook (id, name,phonenumber)2VALUES(1, 'Alice','704-555-1212');
Thanks for reading…
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.