1

I have a singleton class which manages data and database handling of my application. I create a sqlite3 database with default values and kept it in the asset directory. In one of my APIs of this class I have made a check that if DB doesn't exist in /data/data//my.db then copy it from asset directory to this location.

Given my class is singleton and doesn't inherit from UI classes, Is this possible that I can write this API in my current setup or I have to rethink some alternate design?

Class DBHandler {

// singleton class

public void initDatabase() {

   File dbFile = new File("/data/data/<mypackage/a.db>");
   if (dbFile.exists()) {
    return;
   }

   //copy database from asset directory

   //How to do this here?????

}

};

I want to know is this possible??

Waynn Lue
  • 11,344
  • 8
  • 51
  • 76
Rakesh Singh
  • 858
  • 1
  • 12
  • 31

1 Answers1

3

Pass a Context reference to initDatabase(), then you can use its methods to acceess files and databases in your app's private directory.

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • Did you meant to declare a context in the current class or pass it from some activity class?? – Rakesh Singh Sep 10 '11 at 08:38
  • And also if I have to pass the context from some Activity then is it possible to pass the context as reference: The below link shows parameters in java is always happen by call by value: http://stackoverflow.com/questions/40480/is-java-pass-by-reference – Rakesh Singh Sep 10 '11 at 08:50
  • Pass it from your activity. You don't need to change it, and it _is_ a reference, so there is no issue. – Nikolay Elenkov Sep 10 '11 at 15:42