lundi 7 mars 2016

SQLiteException: file is encrypted or is not a database: create locale table failed in android 6.0

I have a problem:

CREATE TABLE android_metadata failed 
    Failed to setLocale() when constructing, closing the database 
    net.sqlcipher.database.SQLiteException: file is encrypted or is not a database: create locale table failed

My DBHelper class for Sqlcipher:

public class DBHelper extends SQLiteOpenHelper implements DataStore {
    private static DBHelper sInstance;
    private String DBKey;

    private DBHelper(Context context) {
        super(context, DBConst.DB_NAME, null, DBConst.DB_VERSION);
    }

    public static DBHelper getInstance() {
        DBHelper localInstance = sInstance;
        if (localInstance == null) {
            synchronized (DBHelper.class) {
                localInstance = sInstance;
                if (localInstance == null) {
                    SQLiteDatabase.loadLibs(App.getContext());
                    sInstance = localInstance = new DBHelper(App.getContext());
                }
            }
        }
        return localInstance;
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(createTable(DBConst.TABLE_NAME));
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(dropTable(DBConst.TABLE_NAME));
        onCreate(db);
    }

    private String createTable(String name) {
        return "CREATE TABLE IF NOT EXISTS  " + name + " (" +
                DBConst.DB_ID + " integer primary key," +
                DBConst.DB_DATA + " text);";
    }

    private String dropTable(String name) {
        return "DROP TABLE IF EXISTS " + name;
    }

    private String getDBKey() {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            DBKey = "Kostilniy kly4 dlya android M";
        }
        if (DBKey == null)
            DBKey = PreferencesStorage.getInstance().getDBKey();
        return DBKey;
    }

    public synchronized void deleteTable(String name) {
        SQLiteDatabase db = getWritableDatabase(getDBKey());
        db.delete(name, null, null);
        db.close();
    }

    @Override
    public synchronized void writeData(NoSqlEntity entity, String tableName) {
        SQLiteDatabase db = getWritableDatabase(getDBKey());
        ContentValues cv = new ContentValues();
        cv.put(DBConst.DB_ID, entity.entityId);
        cv.put(DBConst.DB_DATA, entity.entityData);
        db.insert(tableName, null, cv);
        db.close();
    }

    @Override
    public synchronized <T> List<T> readAllData(Class<T> clazz, String tableName) {
        ArrayList<T> list = new ArrayList<>();
        SQLiteDatabase db = getWritableDatabase(getDBKey());
        Cursor c = db.query(tableName,
                new String[]{
                        DBConst.DB_ID, DBConst.DB_DATA
                }, null, null, null, null, null);

        if (c.moveToFirst()) {
            do {
                list.add(new GsonSerialization().deserializeFromDB(c.getString(c.getColumnIndex(DBConst.DB_DATA)), clazz));
            } while (c.moveToNext());
        }
        c.close();
        db.close();
        return list;
    }
}

There is no problem if the key to the database is hard-coded into a constant.

From the Preferences Storage I get the correct key.

What am I doing wrong?

I think this problem appeared in android 6.0.

Aucun commentaire:

Enregistrer un commentaire