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 D
You can get path by context.getDatabasePath(DataBaseHelper.dbName)
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! :-)
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.
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();
}