0

I have around 2600 entries in my SQLite database. I need to delete all those entries and put in updated entries. I know that the database can be updated by changing the database version in the DatabaseHandler class, but that can only be done if the whole app is updated. I need to programatically update the database. Can this be done? I searched but couldn't find a satisfactory answer. Thanks.

6
  • If the schema is the same, the version should be the same, no? Just delete and add the records as appropriate. Consider using a simple in-database "last version of data" token (which works well for "get all latest" synchronization with external sources). Commented Jun 23, 2013 at 19:05
  • You can replace databases on the fly. What have you tried? Commented Jun 23, 2013 at 19:09
  • @user2246674 Is there a more elegant way to update the whole database, instead of updating values one by one? Commented Jun 23, 2013 at 19:10
  • What is wrong with "one by one"? Obviously a loop would be used - and inside a single transaction, multiple inserts are very fast. (An insert can be fed multiple records at a time, but it won't make a noticeable difference for SQLite when transactions are used and it makes the looping/building code more complex.) Also, if an entire external database is downloaded, it can be attached and the records can be pulled over in one statement. It really depends on where the data comes from, etc. Commented Jun 23, 2013 at 19:11
  • 1
    Looks like a good start. Make sure to commit the transaction. Commented Jun 23, 2013 at 19:23

1 Answer 1

1

I guess you can call a method some what like this

public void emptyDatabase() {
    db = this.getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_Your_Tbl);
}

Even this will be able to delete those values quickly. Without updating the version number.

Plus as you mentioned that you have to delete the records and add new values. It will be much faster that updating those values.

Sign up to request clarification or add additional context in comments.

2 Comments

If I add those values to rows which already contain data, will the rows get overwritten or appended?
It depends on what structure you used. If you have used Primary Key Auto increment then you can insert but after that you will find duplicate data. So better to Delete first then Add newer one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.