gpt4 book ai didi

android - 获取 "sqlite3_open_v2("mydatabase.db", &handle, 6, NULL) failed"after using sqlcipher in Android

转载 作者:行者123 更新时间:2023-11-29 22:02:08 25 4
gpt4 key购买 nike

在 Android 2.3.3 中使用 sqlcipher 时。我收到错误消息。

这是我的 DB.java 类

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteDatabase.CursorFactory;
import net.sqlcipher.database.SQLiteOpenHelper;

public class DB {

private static String _DB_NAME = "mydatabase.db";
private static int _DB_VERSION = 1;

private Context _context;
private DBHelper _helper;

public DB(Context context) {
SQLiteDatabase.loadLibs(context);
this._context = context;
this._helper = new DBHelper(this._context, _DB_NAME, null, _DB_VERSION);
SQLiteDatabase sqdb = this._helper.getWritableDatabase("123");
sqdb.close();
}

public void insertValuesInTable() {

try {
SQLiteDatabase sqdb = SQLiteDatabase.openOrCreateDatabase(_DB_NAME, "123",null);
sqdb.execSQL("insert into mytable(category, total) values (1,"100");");
sqdb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static class DBHelper extends SQLiteOpenHelper {

public DBHelper(Context context, String name, CursorFactory factory,
int version) {

super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {

createTables(db);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

private void createTables(SQLiteDatabase db) {

try {
String query1;

query1 = "Create table if not exists mytable (category integer primary key, total text) ; ";
db.execSQL(query1);

} catch (Exception e) {
e.printStackTrace();
}
}
}


}

我创建了 DB.java 的对象并像这样使用它

DB _db = new DB(getApplicationContext());
_db.insertValuesInTable();

只要我执行这一行 _db.insertValuesInTable(); 我就会收到这个错误

sqlite returned: error code = 14, msg = cannot open file at line 32288 of [6d326d44fd]
sqlite returned: error code = 14, msg = os_unix.c:32288: (2) open(//mydatabase.db) -
sqlite3_open_v2("mydatabase.db", &handle, 6, NULL) failed
net.sqlcipher.database.SQLiteException: unable to open database file
net.sqlcipher.database.SQLiteDatabase.dbopen(Native Method)
net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1952)
net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:902)
net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:945)
com.keane.database.DB.insertValuesInMd5Check(DB.java:133)
com.keane.test2.UsageMonitorTestActivity.onCreate(UsageMonitorTestActivity.java:45)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
android.app.ActivityThread.access$1500(ActivityThread.java:117)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:123)
android.app.ActivityThread.main(ActivityThread.java:3683)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:507)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
dalvik.system.NativeStart.main(Native Method)

我已正确包含所有 .so.jar.zip 文件。我仍然收到此错误。如何解决此问题?

最佳答案

您应该创建一个 android.app.Application 的子类,并在您的 AndroidManifest.xml 文件中注册它。在调用 SQLiteDatabase.openOrCreateDatabase 之前,通过从应用程序实例调用 getDatabasePath 获取数据库文件的完整路径,并将该值传递给 SQLiteDatabase.openOrCreateDatabase.

关于android - 获取 "sqlite3_open_v2("mydatabase.db", &handle, 6, NULL) failed"after using sqlcipher in Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11648177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com