我的资产文件夹中有一个数据库。我已尝试使用以下代码加载数据库。
public SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = SQLiteDatabase.openOrCreateDatabase(new File("file:///android_asset/e2b.db"), null);
Toast.makeText(getBaseContext(), "Error" + db.isOpen(), Toast.LENGTH_LONG).show();
}
我试图打开它。 Hasn尚未使用它。但是,当我运行我的应用程序时,窗口不会弹出以下弹出窗口(错误)
不幸的是,MyDBApp已停止
LogCat位于下方。
05-12 13:10:01.673 16004 16004 I art Late-enable -Xcheck:jni 05-12 13:10:01.877 16004 16004 W System ClassLoader引用未知路径:/data/app/com.aashakil.e2b2edict-1/lib/arm 05-12 13:10:02.227 16004 16004 D AccessibilityManager getInstance()new sInstance = android.view.accessibility.AccessibilityManager@ec2dd0e,context = com.aashakil.e2b2edict.MainActivity@f26932f,userId = 0 05-12 13:10:02.288 16004 16004 E SQLiteLog(14)无法打开[2ef4f3a5b1]第31278行的文件 05-12 13:10:02.288 16004 16004 E SQLiteLog(14)os_unix.c:31278:(2)open(// file:/android_asset/e2b.db) - 05-12 13:10:02.331 16004 16004 E SQLiteDatabase无法打开数据库'文件:/android_asset/e2b.db'。 05-12 13:10:02.331 16004 16004 E SQLiteDatabase android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码14):无法打开数据库 05-12 13:10:02.331 16004 16004 E androiditeDatabase.sqlite.SQLiteConnection.nativeOpen上的SQLiteDatabase(本机方法) 05-12 13:10:02.331 16004 16004 E androiditeDatabase.sqlite.SQLiteConnection.open上的SQLiteDatabase(SQLiteConnection.java:207) 05-12 13:10:02.331 16004 16004 E androiditeDatabase.sqlite.SQLiteConnection.open上的SQLiteDatabase(SQLiteConnection.java:191) 05-12 13:10:02.331 16004 16004 E androiditeDatabase.sqlite.SQLiteConnectionPool.openConnectionLocked上的SQLiteDatabase(SQLiteConnectionPool.java:463) 05-12 13:10:02.331 16004 16004 E androiditeDatabase.sqlite.SQLiteConnectionPool.open上的SQLiteDatabase(SQLiteConnectionPool.java:185) 05-12 13:10:02.331 16004 16004 E androiditeDatabase.sqlite.SQLiteConnectionPool.open上的SQLiteDatabase(SQLiteConnectionPool.java:177) 05-12 13:10:02.331 16004 16004 E androiditeDatabase.sqlite.SQLiteDatabase.openInner上的SQLiteDatabase(SQLiteDatabase.java:806) 05-12 13:10:02.331 16004 16004 E androiditeDatabase.sqlite.SQLiteDatabase.open上的SQLiteDatabase(SQLiteDatabase.java:791) 05-12 13:10:02.331 16004 16004 E androidite.database.sqlite.SQLiteDatabase.openDatabase上的SQLiteDatabase(SQLiteDatabase.java:694) 05-12 13:10:02.331 16004 16004 E androiditeDatabase.sqlite.SQLiteDatabase.openOrCreateDatabase上的SQLiteDatabase(SQLiteDatabase.java:709) 05-12 13:10:02.331 16004 16004 E androiditeDatabase.sqlite.SQLiteDatabase.openOrCreateDatabase中的SQLiteDatabase(SQLiteDatabase.java:702) 05-12 13:10:02.331 16004 16004 E SQLiteDatabase at com.aashakil.e2b2edict.MainActivity.onCreate(MainActivity.java:20) 05-12 13:10:02.331 16004 16004 E SQLiteDatabase at android.app.Activity.performCreate(Activity.java:6251) 05-12 13:10:02.331 16004 16004 E SQLiteDatabase at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) 05-12 13:10:02.331 16004 16004 E SQLiteDatabase at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2370) 05-12 13:10:02.331 16004 16004 E SQLiteDatabase at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2477) 05-12 13:10:02.331 16004 16004 E SQLiteDatabase at android.app.ActivityThread.-wrap11(ActivityThread.java) 05-12 13:10:02.331 16004 16004 E SQLiteDatabase at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1345) 05-12 13:10:02.331 16004 16004 E SQLiteDatabase at android.os.Handler.dispatchMessage(Handler.java:102) 05-12 13:10:02.331 16004 16004 E androidite数据库在android.os.Looper.loop(Looper.java:148) 05-12 13:10:02.331 16004 16004 E在android.app.ActivityThread.main上的SQLiteDatabase(ActivityThread.java:5452) 05-12 13:10:02.331 16004 16004 E javaiteDatabase at java.lang.reflect.Method.invoke(Native Method) 05-12 13:10:02.331 16004 16004 E SQLiteDatabase at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:762) 05-12 13:10:02.331 16004 16004 E SQLiteDatabase at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 05-12 13:10:02.331 16004 16004 D AndroidRuntime关闭VM 05-12 13:10:02.333 16004 16004 E AndroidRuntime致命例外:主要 05-12 13:10:02.333 16004 16004 E AndroidRuntime进程:com.aashakil.e2b2edict,PID:16004 05-12 13:10:02.333 16004 16004 E AndroidRuntime java.lang.RuntimeException:无法启动活动ComponentInfo {com.aashakil.e2b2edict / com.aashakil.e2b2edict.MainActivity}:android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码14):无法打开数据库 05-12 13:10:02.333 16004 16004 E AndroidRuntime在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2477) 05-12 13:10:02.333 16004 16004 E AndroidRuntime在android.app.ActivityThread.-wrap11(ActivityThread.java) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1345) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:102) 05-12 13:10:02.333 16004 16004 E androidRuntime at android.os.Looper.loop(Looper.java:148) 05-12 13:10:02.333 16004 16004 E AndroidRuntime在android.app.ActivityThread.main(ActivityThread.java:5452) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at java.lang.reflect.Method.invoke(Native Method) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:762) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 05-12 13:10:02.333 16004 16004 E AndroidRuntime引起:android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码14):无法打开数据库 05-12 13:10:02.333 16004 16004 E AndroidRuntime at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:207) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191) 05-12 13:10:02.333 16004 16004 E androidRuntime at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) 05-12 13:10:02.333 16004 16004 E androidRuntime at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806) 05-12 13:10:02.333 16004 16004 E androidRuntime at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:709) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:702) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at com.aashakil.e2b2edict.MainActivity.onCreate(MainActivity.java:20) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at android.app.Activity.performCreate(Activity.java:6251) 05-12 13:10:02.333 16004 16004 E AndroidRuntime at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) 05-12 13:10:02.333 16004 16004 E AndroidRuntime在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2370)
答案 0 :(得分:1)
file:///android_asset
网址仅适用于WebView
,而不适用于一般的文件操作。
要使用资产中的数据库文件,需要将其复制到文件系统。您可以使用sqlite-asset-helper来执行此操作。
答案 1 :(得分:0)
尝试使用不同的openOrCreateDatabase
方法重载变体:
openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);
答案 2 :(得分:0)
您无需调用片段中的openOrCreateDatabase
,而只需调用
活动中:
SQLiteDatabase db = openOrCreateDatabase("DATABASE",android.content.Context.MODE_PRIVATE ,null);
Fragment中的正确方法:
SQLiteDatabase db = getActivity().openOrCreateDatabase("DATABASE",android.content.Context.MODE_PRIVATE ,null);**