I want to use a "pre loaded" database in my app. There are tons of questions about this and most point to this blog article here or similars.
我想在我的应用程序中使用“预加载”数据库。关于这一点有很多问题,大多数都指向这篇博客文章或类似的。
So far so good. I just want to know if there is a better way to get the default databases directory so you don't have to use something like this:
到现在为止还挺好。我只是想知道是否有更好的方法来获取默认数据库目录,所以你不必使用这样的东西:
private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
I mean, maybe that is changed in the future or maybe a device or rom could place it elsewhere... so is there a way to get this path programatically?
我的意思是,未来可能会改变,或者设备或ROM可能会将其放在其他地方......那么有没有办法以编程方式获取此路径?
In Context exists a method to getDatabasePath(name), but you need to give it an existing db name and well... it doesn't exist yet, I want to move it there :P
在Context中存在一个getDatabasePath(name)的方法,但你需要给它一个现有的db名称,而且......它还不存在,我想把它移到那里:P
5 个解决方案
#1
7
Create an empty DB, get the path with getDatabasePath()
, then overwrite it with your own.
创建一个空数据库,使用getDatabasePath()获取路径,然后用自己的数据覆盖它。
#2
10
I used...
我用了...
String destPath = getFilesDir().getPath();
destPath = destPath.substring(0, destPath.lastIndexOf("/")) + "/databases";
#3
7
Used by SQLiteAssetHelper:
由SQLiteAssetHelper使用:
String path = mContext.getDatabasePath(mName).getPath();
At this time, the database doesn't exist. I think the String just takes the internal path and adds the appropriate modifiers. In fact, this seems to work just fine:
这时,数据库不存在。我认为String只是采用内部路径并添加适当的修饰符。事实上,这似乎工作得很好:
context.getDatabasePath("a").getParentFile()
Basically, you don't need to have a real database created, just ask it for one.
基本上,您不需要创建一个真正的数据库,只需要一个。
#4
6
You can use the Method getFilesDir()
or getDatabasePath
in an Activity-Class to get this Folder.
More info here
您可以在Activity-Class中使用方法getFilesDir()或getDatabasePath来获取此文件夹。更多信息在这里
#5
1
You can use getDatabasePath method in your Helper class:
您可以在Helper类中使用getDatabasePath方法:
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);
// you can use an alternate constructor to specify a database location
// (such as a folder on the sd card)
// you must ensure that this folder is available and you have permission
// to write to it
// super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION);
databasePath = context.getDatabasePath("wl.db").getPath();
}
#1
7
Create an empty DB, get the path with getDatabasePath()
, then overwrite it with your own.
创建一个空数据库,使用getDatabasePath()获取路径,然后用自己的数据覆盖它。
#2
10
I used...
我用了...
String destPath = getFilesDir().getPath();
destPath = destPath.substring(0, destPath.lastIndexOf("/")) + "/databases";
#3
7
Used by SQLiteAssetHelper:
由SQLiteAssetHelper使用:
String path = mContext.getDatabasePath(mName).getPath();
At this time, the database doesn't exist. I think the String just takes the internal path and adds the appropriate modifiers. In fact, this seems to work just fine:
这时,数据库不存在。我认为String只是采用内部路径并添加适当的修饰符。事实上,这似乎工作得很好:
context.getDatabasePath("a").getParentFile()
Basically, you don't need to have a real database created, just ask it for one.
基本上,您不需要创建一个真正的数据库,只需要一个。
#4
6
You can use the Method getFilesDir()
or getDatabasePath
in an Activity-Class to get this Folder.
More info here
您可以在Activity-Class中使用方法getFilesDir()或getDatabasePath来获取此文件夹。更多信息在这里
#5
1
You can use getDatabasePath method in your Helper class:
您可以在Helper类中使用getDatabasePath方法:
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);
// you can use an alternate constructor to specify a database location
// (such as a folder on the sd card)
// you must ensure that this folder is available and you have permission
// to write to it
// super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION);
databasePath = context.getDatabasePath("wl.db").getPath();
}