如何访问数据库或不同的Andr​​oid应用程序的文件的路径应用程序、路径、不同、数据库

2023-09-07 17:00:29 作者:一年只遇你

时有可能得到数据库文件的路径Android的路径为:/data/system/accounts.db

在我的应用程序,我想使用这个数据库,但没有得到它的路径。 如果我做硬编码,并删除该文件我能做到这一点。但我想访问数据库,这样我可以删除该表。

任何帮助将是AP preciable。

code我尝试:

 私人SQLiteDatabase分贝;
    文件DBFILE = mContext.getDatabasePath(accounts.db);
    如果(dbFile.exists())
    {
        DB = SQLiteDatabase.openOrCreateDatabase(DBFILE,NULL);
        db.execSQL(DROP TABLE IF EXISTS账户);
        db.setVersion(db.getVersion()+ 1);
    }
 

解决方案

您可以使用 android.os.Environment.getDataDirectory()以获得路径数据目录。 不要使用mContext.getDatabasePath如果db是不是在你的应用程序的数据目录。

另请参阅 http://stackoverflow.com/questions/3436434/is-it-possible-to-move-the-internal-db-to-the-sdcard/3436869#3436869

如果你想搜索,通过这样的事情做一个递归搜索文件:

 私人无效searchFile(字符串起点){

    。文件[]文件=文件(/数据)listFiles();
    对于(F文件:文件){
        如果(f.isDirector)searchFile(F);
        否则,如果(f.getName()。等于(accounts.db)){
            //这里删除数据库...
            //并退出搜索在这里...
        }
    }
}
 

和检查它是否包含accounds.db。如果达到另一个子目录,递归调用自己的函数和子文件夹为出发点,等等....

但一定要设置为起点,开始搜索相关的文件夹您已经阅读访问。

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);
    }

解决方案

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.