0

I am building an application which has a database with two tables created internally using SQLiteOpenHelper.When ever the application is running,it receives some data and saves it into the tables.What I want is to clear the data tables when ever the application is started?

I looked into this post How can I clear an SQLite database each time I start my application? which is not clear of how to use application.

halfer
  • 19,824
  • 17
  • 99
  • 186
ChanChow
  • 1,346
  • 7
  • 28
  • 57
  • Your question isn't clear. Do you wan't to clear the DB each time the user starts the main/launcher `Activity` or only when the 'app' (or 'application') is started? Be aware that an `Activity` is not the same as the generic terms 'app' or 'application'. – Squonk Aug 13 '12 at 23:32
  • I have only one activity for my application. I am building an application using google maps api. I am trying to save gps traces into a database when ever the application starts and I would like to clear the data in the existing database. – ChanChow Aug 15 '12 at 04:57

2 Answers2

2

SQLiteOpenHelper has the ability of creating in-memory databases if you pass the constructor a null name. Probably this is what you are looking for.

For example:

SQLiteOpenHelper sqloh = new SQLiteOpenHelper(context, null, null, 1);
SQLiteDatabase sqldb = sqloh.getWritableDatabase();

will create an in-memory db.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
1

SqliteDatabase uses the method openOrCreate(...) which opens a database if it exist and creates and opens it when it doesn't exist. see docs based on that you could just delete the database file that is created before you do any database calls so that a new one is created each time.

This SO question gives the location of the file: Location of sqlite database on the device

There other route would be just to delete the data in the tables when the application starts by executing a sqlite query:

DELETE FROM your_table

My only thought would be do you really need a database if you are going to delete it every time the application starts. If you are not updating the data then why not just "cache" a json file with the data. The GSON library is awesome for taking json and converting it to java object with very little code, going to be less code than working with sqlite. But the recommendation comes from not having the big picture for what you are trying to accomplish. You then would just delete the json file(s) when the app starts instead of the db file.

Community
  • 1
  • 1
bytebender
  • 7,371
  • 2
  • 31
  • 54
  • If I am using this DELETE FROM ... statement it will not clear the primary key which has auto increment property right? I need even that to be cleared and one more thing is where should I call the delete query from? Should it be placed in OnCreate() function? – ChanChow Aug 16 '12 at 04:06
  • What is the life cycle of onCreate in SQLiteOpenHelper class. will it be called every time we try to access the database created? – ChanChow Aug 16 '12 at 04:47
  • I would give @dtmilano's in memory sqlite database a try... that sounds like the simplest solution. – bytebender Aug 16 '12 at 16:21