views:

138

answers:

1

Is it possible to get path of database file in android at path : "/data/system/accounts.db"

in my app i want to use this database, but not getting its path. if i do hardcoding and remove the file i'm able to do it. But i want to access it as database so that i can drop the table.

Any help would be appreciable.

code i tried:

    private SQLiteDatabase db;
    File dbFile = mContext.getDatabasePath("accounts.db");
    if (dbFile.exists())
    {
        db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
        db.execSQL("DROP TABLE IF EXISTS accounts");
        db.setVersion(db.getVersion()+1);
    }
A: 

You can use android.os.Environment.getDataDirectory() to get the path to the data directory. Don't use the mContext.getDatabasePath if the db is not in the data dir of your app.

Also see http://stackoverflow.com/questions/3436434/is-it-possible-to-move-the-internal-db-to-the-sdcard/3436869#3436869

if you want to search, do a recursive file search via something like this:

private void searchFile(String startingPoint) {

    Files[] files = File("/data").listFiles(); 
    for (File f:files) {
        if (f.isDirector) searchFile(f);
        else if (f.getName().equals("accounts.db")) {
            // delete db here...
            // and exit the search here...
        }
    } 
}

and check if it contains the accounds.db. If you reach another subdirectory, call your own function recursively with the subfolder as the starting point, and so on....

But make sure you have read access to the relevant folder you set as starting point to start the search.

Mathias Lin
Thanks Mathias for ur prompt reply..but using getDataDirectory() i'll not be able to search accounts.db in "/data" directory.can u plz help me on that..
parul
what's the problem you get? FileNotFound or PermissionDenied? The path should be android.os.Environment.getDataDirectory() + File.separator + "system" + File.separator + "accounts.db". You mentioned "if i do hardcoding and remove the file i'm able to do it" ... so what did you hardcode before? Did you compare the hardcoded path with the one I mentioned?
Mathias Lin
if i use "android.os.Environment.getDataDirectory() + File.separator + "system" + File.separator + "accounts.db" its equivalent to hardcoding...earlier i tried : File file = new File("/data/system/accounts.db"); boolean deleted = file.delete();so this file accounts.db i want to search in "/data" and delete rather than harcoding.Can u plz help.
parul
see edited code above
Mathias Lin
thanks Mathias, i tried the same code but its throwing nullpointer exception at this line: for (File f:files)plz suggest how to remove it...
parul
try to debug and check the value of 'files', if it's null. maybe you don't have permission to /data/ directory directly. not sure, I haven't tried the code myself, just a conceptual code idea..or maybe change the path to /data/data/com.yourapppackage/files/depends on the most upper possible directory level where the db could reside.
Mathias Lin