6

在我的应用程序中使用SQLiteAssetHelper时,我在某些设备上遇到了崩溃,尤其是在 OnePlus 设备上。现在我在这里读到它与存储数据库的目录有关。

现在我正在尝试找到一种解决方法,目前我能想到的最好的方法就是这样的构造函数

public MySubclass(Context context) {
    super(context, databaseName, context.getFilesDir() + "/databases", null, databaseVersion);

这是正确的方法还是这种方法有其他问题?

编辑

例外是

Fatal Exception: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version x to y: /data/data/my.package.id/databases/database.db

抱歉,我链接了错误的 SO 问题:这是正确的问题。那里说'OnePlus能够将数据库复制到/data/data/package-name/databases/filename.db,但它不允许访问该数据,我对此一无所知。

4

2 回答 2

2

SQLiteAssetHelper 构造函数允许您指定自己的数据库目标路径(该文件夹需要是可写的)。如果您未指定任何内容,则默认使用一个。请参阅 Github 存储库的摘录:

    if (storageDirectory != null) {
        mDatabasePath = storageDirectory;
    } else {
        mDatabasePath = context.getApplicationInfo().dataDir + "/databases";
    }
于 2015-12-03T14:26:33.357 回答
2
Hey you can copy database from one to another from below mentioned code.

public class MySQLiteHelper extends SQLiteOpenHelper implements DBConstants {

private static MySQLiteHelper mInstance = null;
private SQLiteDatabase myDataBase;
private static String DB_PATH = "";

public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    try {
        if (checkDataBase())
            openDataBase();
        else
            myDataBase = this.getReadableDatabase();
    } catch (Exception e) {
    }
}

public static MySQLiteHelper instance(Context context) {

    File outFile = context.getDatabasePath(DATABASE_NAME);
    DB_PATH = outFile.getPath();

    if (mInstance == null) {
        mInstance = new MySQLiteHelper(context);
    }

    return mInstance;
}
于 2015-12-15T21:54:44.157 回答