Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: failed to setLocale() when constructing, closing the database

Tags:

android

sqlite

I'm trying to create a database with 12 different tables which has been working fine until today. Now every time I start my app for the first time after uninstalling it and re-installing it (or just clear app data) I get the error message seen in the title. However, the second time I start the app after getting this error it works fine. The code bellow is from my DatabaseHelper class which is more or less the same as the android notepad tutorial.

This error comes after the database has been opened in my activity and I try to make my first query.

Any suggestions of what could cause this and how to solve it?

private static class DatabaseHelper extends SQLiteOpenHelper {

    private static String DB_PATH = "/data/data/my_app/databases/";

    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        if (!checkDataBase()) {
            db.execSQL(TABLE_1);
            db.execSQL(TABLE_2);
            db.execSQL(TABLE_3);

            db.execSQL(TABLE_4);
            db.execSQL(TABLE_5);
            db.execSQL(TABLE_6);

            db.execSQL(TABLE_7);
            db.execSQL(TABLE_8);
            db.execSQL(TABLE_9);

            db.execSQL(TABLE_10);
            db.execSQL(TABLE_11);
            db.execSQL(TABLE_12);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_1);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_2);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_3);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_4);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_5);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_6);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_7);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_8);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_9);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_10);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_11);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_12);
        onCreate(db);
    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            checkDB = SQLiteDatabase.openDatabase(DB_PATH + DATABASE_NAME, null, SQLiteDatabase.OPEN_READONLY);
            checkDB.close();
        } catch (SQLiteException e) {

        }
        return checkDB != null ? true : false;
    }
}
like image 330
just_user Avatar asked Oct 04 '11 11:10

just_user


1 Answers

The problem is that your database probably doesn't include Android-related metadata.

You need to use the SQLiteDatabase.NO_LOCALIZED_COLLATORS flag when calling SQLiteDatabase.openDatabase() - this no longer causes the problem. At least it worked for me.

like image 131
gonzobrains Avatar answered Oct 19 '22 12:10

gonzobrains