Navigate back to the homepage

SQLite insert or update statement example

Infinitbility
SQLite
December 2nd, 2021 · 1 min read
SQLite insert or update statement example

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

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.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to create SQLite database at runtime in react native?

How to create SQLite database at runtime in react native?

React Native sqlite runtime database setup example

December 1st, 2021 · 1 min read
How to check if a json object has a key in typescript?

How to check if a json object has a key in typescript?

Example of check object key exist or not in typesacript.

November 30th, 2021 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]