0

In a previous article there is a way to create a single instance of my SQLiteDatabse and then simply reference it throughout my App. I can not figure how to tell it where my database is. Since my database is written to the SDCard, there has got to be a way to tell it which database it should use right? Here is the code below:

    public class DatabaseManager {

    private int mOpenCounter;

    private static DatabaseManager instance;
    private static SQLiteOpenHelper mDatabaseHelper;
    private SQLiteDatabase mDatabase;

    public static synchronized void initializeInstance(SQLiteOpenHelper helper) {
        if (instance == null) {
            instance = new DatabaseManager();
            mDatabaseHelper = helper;
        }
    }

    public static synchronized DatabaseManager getInstance() {
        if (instance == null) {
            throw new IllegalStateException(DatabaseManager.class.getSimpleName() +
                    " is not initialized, call initializeInstance(..) method first.");
        }

        return instance;
    }

    public synchronized SQLiteDatabase openDatabase() {
        mOpenCounter++;
        if(mOpenCounter == 1) {
            // Opening new database
            mDatabase = mDatabaseHelper.getWritableDatabase();
        }
        return mDatabase;
    }

    public synchronized void closeDatabase() {
        mOpenCounter--;
        if(mOpenCounter == 0) {
            // Closing database
            mDatabase.close();

        }
    }

}

My actual database is called FooBase.db

static File directoryPath = new File(Environment.getExternalStorageDirectory().toString() + "/MyApp/database");
Ovi Faur
  • 518
  • 3
  • 19

1 Answers1

0

you don't need to mention the path of the database explicitly.

All the android apps by default store their database(s) in the location /data/data/packageName/

SQLiteOpenHelper - creating database on SD card

How to store sqlite database directly on sdcard

The Docs

Community
  • 1
  • 1
SMR
  • 6,628
  • 2
  • 35
  • 56
  • Thank you but in this case the database will become at at least 13mb or more, so I wanted to be sure there is plenty of space by writing it to the SDCard along with other files as part of the app. So with that, I still must designate the above –  Jul 08 '14 at 10:31
  • additionally, there still has to be a way to tell this which database to open. What if you have many? –  Jul 08 '14 at 10:36
  • Ok... now thats a different case I will update my answer soon. – SMR Jul 08 '14 at 10:40
  • Thank you for your time, but this has nothing to do with my question. I already have a working database and have already created it. I ran into Asynctask problems where the database would be closed while trying to read it. SO I am adding the code above so that I can properly reference one single instance. What the code above is missing, is the database name and location. Otherwise, what is the instance a reference to. –  Jul 08 '14 at 11:12
  • the solution are there in the answers of the links I provided. all you need to do add the path in the `onCreate()` method of the `SQLiteOpenHelper` class and create a directory in the SD card and use it. Additionally you will need READ/Write permission for the SD Card. – SMR Jul 08 '14 at 11:17