javaandroidandroid-studioandroid-sqlitesqlcipher

Two identical SQLiteExceprion in one line


I have two identical errors in one line using SQLCipher.

I am checking the password for correctness. If the parameter is entered incorrectly, two exceptions occur.

I looked at this page to see if the password I entered was correct. When I repeated the code, I still got two exactly the same exceptions on the same line of code. One exception I can catch, the other is logged.

TaskOpenActivity:

        binding.openDatabaseFab.setOnClickListener(new View.OnClickListener() {
            @OptIn(markerClass = ExperimentalBadgeUtils.class)
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MainActivity.class);
                SharedPreferences sharedPreferences = context.getSharedPreferences("TaskCalendarPreferences", Context.MODE_PRIVATE);
                sharedPreferences.edit().putString("PassphraseMD5", TaskApplication.getInstance().md5(binding.passphrase.getText().toString())).commit();

                SharedPreferences preferences = context.getApplicationContext().getSharedPreferences("TaskCalendarPreferences", MODE_PRIVATE);
                String passphrase = preferences.getString("PassphraseMD5", "");
                Log.d("TaskCalendarLog", passphrase);
                try {
                    TaskApplication.getInstance().loadAllData();
                    startActivity(intent);
                    finish();
                } catch (SQLiteException e) {
                    Log.d("TaskCalendarLog", "Incorrect password");
                }
            }
        });

TaskApplication:

    public void loadAllData() {
        taskCalendarDBHelper = new TaskCalendarDBHelper(this);
        try {
            taskCalendarDBHelper.loadAllTasks();
            taskCalendarDBHelper.loadAllNotes();
            taskCalendarDBHelper.loadAllDayAlarms();
        } catch (SQLiteException e) {
            throw e;
        }
    }

TaskBDOpenHelper:

    public void loadAllTasks() {
        SharedPreferences preferences = context.getApplicationContext().getSharedPreferences("TaskCalendarPreferences", MODE_PRIVATE);
        passphrase = preferences.getString("PassphraseMD5", "");
        Log.d("TaskCalendarLog", passphrase);

        TaskManager taskManager = TaskManager.getInstance();
        //SQLiteDatabase actionsDatabase;
        //actionsDatabase = SQLiteDatabase.openDatabase(DATABASE_PATH + DATABASE_NAME, passphrase, null, SQLiteDatabase.OPEN_READONLY, hook);
        try {
            taskCalendarDatabase = SQLiteDatabase.openOrCreateDatabase(DATABASE_PATH + DATABASE_NAME, passphrase, null, null, hook); // <--- Exception here
        } catch (SQLiteException e) {
            throw e;
        }
        //actionsDatabase = this.getReadableDatabase();
        Cursor cursor = taskCalendarDatabase.query(TaskCalendarDBHelper.TASK_TABLE, null, null, null, null, null, null);

        if (cursor.moveToFirst()) {
            int taskInt = cursor.getColumnIndex(TASK);
            int date = cursor.getColumnIndex(DATE);
            int time = cursor.getColumnIndex(TIME);
            int hasAlarm = cursor.getColumnIndex(HAS_ALARM);
            int hasNotification = cursor.getColumnIndex(HAS_NOTIFICATION);
            int isDone = cursor.getColumnIndex(IS_DONE);
            int wasNotifiedIndex = cursor.getColumnIndex(WAS_NOTIFIED);
            int wasAlarmedIndex = cursor.getColumnIndex(WAS_ALARMED);

            boolean wasNotified;
            boolean wasAlarmed;

            int id = taskManager.getMaxId();

            do {
                if (cursor.getInt(wasNotifiedIndex) == 1) {
                    wasNotified = true;
                } else {
                    wasNotified = false;
                }
                if (cursor.getInt(wasAlarmedIndex) == 1) {
                    wasAlarmed = true;
                } else {
                    wasAlarmed = false;
                }

                Task task = new Task(cursor.getString(taskInt), cursor.getString(date), cursor.getString(time), cursor.getString(hasAlarm), cursor.getString(hasNotification), cursor.getString(isDone));
                task.setId(id);
                task.setWasNotified(wasNotified);
                task.setWasAlarmed(wasAlarmed);
                taskManager.getTaskList().add(task);
                //Log.d("TaskCalendarLog", taskManager.getTaskList() + "");
                id++;
            } while (cursor.moveToNext());
            taskManager.setMaxId(id);
        }
        cursor.close();
        taskCalendarDatabase.close();
    }

If you remove the try-catch block, you will get two absolutely identical errors.

2024-09-30 20:11:57.458 18829-18829 SQLiteDatabase          com.andrewpeterson.todocalendar      E  Failed to open database '/data/data/com.andrewpeterson.todocalendar/databases/TaskCalendarDatabase.db'.
                                                                                                    android.database.sqlite.SQLiteException: file is not a database (code 26): , while compiling: SELECT COUNT(*) FROM sqlite_schema;
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:973)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.executeForLong(SQLiteConnection.java:628)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:240)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:202)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:475)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:189)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:181)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.openInner(SQLiteDatabase.java:1028)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.open(SQLiteDatabase.java:1013)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.openDatabase(SQLiteDatabase.java:840)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.openDatabase(SQLiteDatabase.java:812)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:929)
                                                                                                        at com.andrewpeterson.todocalendar.DataBase.TaskCalendarDBHelper.loadAllTasks(TaskCalendarDBHelper.java:194)
                                                                                                        at com.andrewpeterson.todocalendar.Application.TaskApplication.loadAllData(TaskApplication.java:98)
                                                                                                        at com.andrewpeterson.todocalendar.Activities.OpenDatabaseActivity$1.onClick(OpenDatabaseActivity.java:61)
                                                                                                        at android.view.View.performClick(View.java:7184)
                                                                                                        at android.view.View.performClickInternal(View.java:7157)
                                                                                                        at android.view.View.access$3500(View.java:821)
                                                                                                        at android.view.View$PerformClick.run(View.java:27660)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:914)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:100)
                                                                                                        at android.os.Looper.loop(Looper.java:225)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7563)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:994)
2024-09-30 20:11:57.458 18829-18829 TaskCalendarLog         com.andrewpeterson.todocalendar      D  Incorrect password

UPD:

This exceptions show in log after removing all try-catch blocks and trying to open db with incorrect password.

    2024-10-01 10:27:12.848 23502-23502 SQLiteConnection        com.andrewpeterson.todocalendar      I  Database keying operation returned:0
2024-10-01 10:27:13.421 23502-23502 sqlcipher               com.andrewpeterson.todocalendar      D  ERROR CORE sqlcipher_page_cipher: hmac check failed for pgno=1
2024-10-01 10:27:13.421 23502-23502 sqlcipher               com.andrewpeterson.todocalendar      D  ERROR CORE sqlite3Codec: error decrypting page 1 data: 1
2024-10-01 10:27:13.421 23502-23502 sqlcipher               com.andrewpeterson.todocalendar      D  ERROR CORE sqlcipher_codec_ctx_set_error 1
2024-10-01 10:27:13.421 23502-23502 SQLiteLog               com.andrewpeterson.todocalendar      E  (26) file is not a database in "SELECT COUNT(*) FROM sqlite_schema;"
2024-10-01 10:27:13.427 23502-23502 SQLiteDatabase          com.andrewpeterson.todocalendar      E  Failed to open database '/data/data/com.andrewpeterson.todocalendar/databases/TaskCalendarDatabase.db'.
                                                                                                    android.database.sqlite.SQLiteException: file is not a database (code 26): , while compiling: SELECT COUNT(*) FROM sqlite_schema;
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:973)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.executeForLong(SQLiteConnection.java:628)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:240)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:202)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:475)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:189)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:181)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.openInner(SQLiteDatabase.java:1028)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.open(SQLiteDatabase.java:1013)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.openDatabase(SQLiteDatabase.java:840)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.openDatabase(SQLiteDatabase.java:812)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:929)
                                                                                                        at com.andrewpeterson.todocalendar.DataBase.TaskCalendarDBHelper.loadAllTasks(TaskCalendarDBHelper.java:193)
                                                                                                        at com.andrewpeterson.todocalendar.Application.TaskApplication.loadAllData(TaskApplication.java:98)
                                                                                                        at com.andrewpeterson.todocalendar.Activities.OpenDatabaseActivity$1.onClick(OpenDatabaseActivity.java:60)
                                                                                                        at android.view.View.performClick(View.java:7317)
                                                                                                        at android.view.View.performClickInternal(View.java:7291)
                                                                                                        at android.view.View.access$3600(View.java:838)
                                                                                                        at android.view.View$PerformClick.run(View.java:28247)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:900)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                                                        at android.os.Looper.loop(Looper.java:219)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8676)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)
2024-10-01 10:27:13.427 23502-23502 AndroidRuntime          com.andrewpeterson.todocalendar      D  Shutting down VM
2024-10-01 10:27:13.431 23502-23502 QarthLog                com.andrewpeterson.todocalendar      I  [PatchStore] createDisableExceptionQarthFile
--------- beginning of crash
2024-10-01 10:27:13.431 23502-23502 QarthLog                com.andrewpeterson.todocalendar      I  [PatchStore] create disable file for com.andrewpeterson.todocalendar uid is 10246
2024-10-01 10:27:13.432 23502-23502 AndroidRuntime          com.andrewpeterson.todocalendar      E  FATAL EXCEPTION: main
                                                                                                    Process: com.andrewpeterson.todocalendar, PID: 23502
                                                                                                    android.database.sqlite.SQLiteException: file is not a database (code 26): , while compiling: SELECT COUNT(*) FROM sqlite_schema;
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:973)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.executeForLong(SQLiteConnection.java:628)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:240)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:202)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:475)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:189)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:181)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.openInner(SQLiteDatabase.java:1028)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.open(SQLiteDatabase.java:1013)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.openDatabase(SQLiteDatabase.java:840)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.openDatabase(SQLiteDatabase.java:812)
                                                                                                        at net.zetetic.database.sqlcipher.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:929)
                                                                                                        at com.andrewpeterson.todocalendar.DataBase.TaskCalendarDBHelper.loadAllTasks(TaskCalendarDBHelper.java:193)
                                                                                                        at com.andrewpeterson.todocalendar.Application.TaskApplication.loadAllData(TaskApplication.java:98)
                                                                                                        at com.andrewpeterson.todocalendar.Activities.OpenDatabaseActivity$1.onClick(OpenDatabaseActivity.java:60)
                                                                                                        at android.view.View.performClick(View.java:7317)
                                                                                                        at android.view.View.performClickInternal(View.java:7291)
                                                                                                        at android.view.View.access$3600(View.java:838)
                                                                                                        at android.view.View$PerformClick.run(View.java:28247)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:900)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                                                        at android.os.Looper.loop(Looper.java:219)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8676)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)
2024-10-01 10:27:13.456 23502-23502 Process                 com.andrewpeterson.todocalendar      I  Sending signal. PID: 23502 SIG: 9
---------------------------- PROCESS ENDED (23502) for package com.andrewpeterson.todocalendar ----------------------------

Sorry for my bad English. It's Google Translate.


Solution

  • I got it! It's OK, because Android core SQLiteDatabase.java output exception to log before throw it to me (I don't know why):

    screen

        private void open() {
            try {
                try {
                    openInner();
                } catch (SQLiteDatabaseCorruptException ex) {
                    onCorruption();
                    openInner();
                }
            } catch (SQLiteException ex) {
                Log.e(TAG, "Failed to open database '" + getLabel() + "'.", ex); //<-- Put to the log before catch in my code.
                close();
                throw ex;
            }
        }