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.
-
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).user2246674– user22466742013-06-23 19:05:47 +00:00Commented Jun 23, 2013 at 19:05
-
You can replace databases on the fly. What have you tried?Andrei– Andrei2013-06-23 19:09:07 +00:00Commented 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?vergil corleone– vergil corleone2013-06-23 19:10:44 +00:00Commented 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.user2246674– user22466742013-06-23 19:11:49 +00:00Commented Jun 23, 2013 at 19:11
-
1Looks like a good start. Make sure to commit the transaction.user2246674– user22466742013-06-23 19:23:26 +00:00Commented Jun 23, 2013 at 19:23
|
Show 1 more comment
1 Answer
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.
2 Comments
vergil corleone
If I add those values to rows which already contain data, will the rows get overwritten or appended?
Ashish Vora
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.