View Sidebar
Insert or Update entries in SQLite Database

Insert or Update entries in SQLite Database

April 30, 2014 17:10

Often when working with SQLite Database in Mobile Applications you require to check whether the data row already exist or not. Depending on which if the data is already present you could UPDATE the row or INSERT if not present. The solution is to use REPLACE query to do this, REPLACE will automatically manage the operation of insert if not present else update.

For REPLACE to work properly it requires a UNIQUE INDEX entry in the table, which can be your primary key. So after table is created you could assign a UNIQUE INDEX to it using following command

“CREATE UNIQUE INDEX ID ON EmpTab(EmpID)”

Here a UNIQUE INDEX called “ID” is created on table EmpTab on the column “EmpID”

If you like the article please share

Leave a reply

You must be logged in to post a comment.