29

Im new to android and I want to create kind of CRUD with SQLite.

I have a public class DataBaseHelper which have a constructor:

public DataBaseHelper(Context context)
{
    this.context = context;
}

and a getter for datebase:

public SQLiteDatabase getDataBase() {
    if (dataBase == null) {
        try {
            dataBase = context.openOrCreateDatabase(DataBaseHelper.dbName,
                                                    Context.MODE_PRIVATE, null);

            dataBase.execSQL("CREATE TABLE " + DataBaseHelper.accountTblName + " " +
                                        "(ID integer PRIMARY KEY AUTOINCREMENT" +
                                        ",name TEXT" +
                                        ",address TEXT" +
                                        ",password TEXT)");
            }
            catch(Exception e) {
                Log.e(context.toString(), e.getMessage());

                if (dataBase != null) {
                    dataBase.close();
                }
            }
    }       
    return dataBase;
}

If I start a program for the first time - everything seems to be okay. database is null and I create it (with a table). But when I restart my app database is null again and table cannot be created.

I've decided to write a checkDB function, which tries to open a database. But where can I get a path for it? I dont want it to be "hardcoded".

Or may be I'm doing some kind of anti-pattern?

sfmirtalebi
  • 370
  • 7
  • 16
user1284151
  • 875
  • 4
  • 12
  • 23

4 Answers4

72

You can get path by context.getDatabasePath(DataBaseHelper.dbName)

frogatto
  • 28,539
  • 11
  • 83
  • 129
Ethan Liou
  • 1,622
  • 13
  • 12
5

To get the path of Sqlite data base

here I am writing two ways to get the path.

(1) using custom method

public String getDbPath(Context context,String YourDbName)
{
    return context.getDatabasePath(YourDbName).getAbsolutePath();
}

(2) using File class

  File path=context.getDatabasePath("YourDbName");
  String db_path=path.getAbsolutePath();
  Log.i("Path:",db_path);

even you will see the both code ...then both code is same, 1st one is the shortcut method to get the database path, and it's occupy the less memory compare to second option.

Community
  • 1
  • 1
Abdul Rizwan
  • 3,904
  • 32
  • 31
3

You can use getDatabasePath method inside your helper's constructor:

public class MyDatabase extends SQLiteAssetHelper {

    private static final String DATABASE_NAME = "wl.db";
    private static final int DATABASE_VERSION = 1;  
    public String databasePath = "";    

    public MyDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        databasePath = context.getDatabasePath("wl.db").getPath();
    }
live-love
  • 48,840
  • 22
  • 240
  • 204
0

I needed to access dbs for the app also. This worked for me:

String dbFile;
String[] dbNames = getSherlockActivity().databaseList(); // or ContextWrapper
for (int i = 0; i < dbNames.length; i++) {
    dbFile = getSherlockActivity().getDatabasePath(dbNames[i]).toString();
    Log.i(LOG_TAG, dbFile);
}

Hope it helps! :-)

MSquare
  • 6,311
  • 4
  • 31
  • 37