我使用 SQLCipher 在一行中出现两个相同的错误。
我正在检查密码是否正确。如果参数输入错误,会出现两种异常。
我查看了这个页面,看看我输入的密码是否正确。当我重复代码时,我仍然在同一行代码上得到两个完全相同的异常。我可以捕获一个异常,另一个已记录。
任务打开活动:
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");
}
}
});
任务申请:
public void loadAllData() {
taskCalendarDBHelper = new TaskCalendarDBHelper(this);
try {
taskCalendarDBHelper.loadAllTasks();
taskCalendarDBHelper.loadAllNotes();
taskCalendarDBHelper.loadAllDayAlarms();
} catch (SQLiteException e) {
throw e;
}
}
任务BDOpenHelper:
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();
}
如果删除 try-catch 块,您将得到两个完全相同的错误。
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
更新:
删除所有 try-catch 块并尝试使用不正确的密码打开数据库后,此异常会显示在日志中。
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 ----------------------------
抱歉我的英语不好。这是谷歌翻译。