6

I'm experiencing crashes on some devices when using SQLiteAssetHelper in my app, most of all on OnePlus devices. Now I read here that it has to do with the directory the database is stored.

Now I'm trying to find a workaround, the best I could come up with currently is a constructor like this

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

Is this the correct way to do it or are there other problems with this approach?

EDIT

The exception is

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

Sorry, I linked the wrong SO-question: this is the correct one. There it says 'OnePlus is able to copy the database to /data/data/package-name/databases/filename.db but it doesn't allow to access that data and i have no clue regarding that.'

Community
  • 1
  • 1
swalkner
  • 16,679
  • 31
  • 123
  • 210

2 Answers2

2

The SQLiteAssetHelper constructor allows you to specify your own database destination path (the folder needs to be writable). If you don't specify any it is using one by default. See this excerpt from the Github repo:

    if (storageDirectory != null) {
        mDatabasePath = storageDirectory;
    } else {
        mDatabasePath = context.getApplicationInfo().dataDir + "/databases";
    }
narko
  • 3,645
  • 1
  • 28
  • 33
  • yes, I know, that's why I'm now using the constructor you see in my question. I'm just not sure if it's correct that way, especially the directory I'm using. – swalkner Dec 03 '15 at 14:50
  • May I ask you for the exception you get when using "context.getApplicationInfo().dataDir"? I didn't try myself with getFilesDir, so I speculate here. It might be that your app stops working if you move it to the sdcard for example. – narko Dec 03 '15 at 15:05
  • According to that message it seems you are trying to upgrade your db file. Is this what you are doing or setting up the db for the first time? – narko Dec 03 '15 at 16:04
  • but I'm setting ```setForcedUpgrade()``` - and this problem appears on only three device types, OnePlus makes 97 % of them. – swalkner Dec 04 '15 at 10:35
  • Can you try saving your db in a different folder that you know for sure you can read/write rather than **/data/data/my.package.id/databases/database.db**? Maybe that specific device is applying some kind of data protection rule to the data folder. – narko Dec 07 '15 at 11:51
  • yes, but that would be the next question; what is a directory I can be sure that every device can read/write and that's suitable for such a task? – swalkner Dec 07 '15 at 12:49
  • I am just saying for the sake of debugging and not as a final solution. Maybe the media folder? I am thinking that maybe the file "database.db" doesn't have write grants. – narko Dec 07 '15 at 12:58
  • 1
    I do not have a OnePlus Two at hand unfortunately, therefore I can't debug to find the correct folder... getReadableDatabase() doesn't work either – swalkner Dec 09 '15 at 04:22
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;
}
Amit Sinha
  • 93
  • 4
  • If you want to copy paste others answer at least do it completely and give the original user credits :-| P.S: The original answer [link](http://stackoverflow.com/a/34260857/3125800) – Build3r Jan 27 '16 at 14:46