0

In my application, I want to delete my existing database and create a new one with default values. Default values can be inserted to the database from XML.

Does anyone have any idea on how to reuse a database?

apaderno
  • 28,547
  • 16
  • 75
  • 90

2 Answers2

1

Assuming that you are using a SQLite database, to delete all the rows from all of your tables inside your database you can use db.execSQL() and heed the advice from this question Drop all tables command:

You can do it with the following DANGEROUS commands:

 PRAGMA writable_schema = 1;
 delete from sqlite_master where type = 'table';
 PRAGMA writable_schema = 0;

you then want to recover the deleted space with

 VACUUM

and a good test to make sure everything is ok

 PRAGMA INTEGRITY_CHECK;

If you haven't written a way to read your XML data yet, this is excellent reading: Store parsed xml data to sqlite ? Android

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
0

Well basically that's not an Android specific question.

Firstly when do you want to recreate the database with default values and how to trigget it.

  • In an UI event like button click etc?
  • Or when you start/stop or destroy your activity?

In any cases you need to drop the database and recreate the whole structure (tables,relationships etc.) again.

Serdar Dogruyol
  • 5,147
  • 3
  • 24
  • 32