Saya percaya bahwa database sebenarnya telah berhasil disalin dan dimuat (dibuka) dan masalahnya adalah database yang disalin tidak memiliki tabel bernama Dictionary1 saat mencoba yang berikut:-
Cursor cursor = sd.query("Dictionary1" ,null, null, null, null, null, null);
Sesuai at com.elytelabs.myapplication.MainActivity.fetchData(MainActivity.java:142)
Jadi Anda perlu memperbaiki nama tabel agar sesuai dengan tabel di database yang disalin .
Anda mungkin ingin menggunakan Tanya Jawab ini untuk menampilkan informasi basis data.
Pesannya :-
11-22 23:14:56.455 13193-13193/com.elytelabs.myapplication E/SQLiteLog: (14) cannot open file at line 30052 of [b3bb660af9]
11-22 23:14:56.455 13193-13193/com.elytelabs.myapplication E/SQLiteLog: (14) os_unix.c:30052: (2) open(/data/data/com.elytelabs.myapplication/databases/dictionary.db) -
11-22 23:14:56.455 13193-13193/com.elytelabs.myapplication E/SQLiteDatabase: Failed to open database '/data/data/com.elytelabs.myapplication/databases/dictionary.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
etc .........
hasil dari saat checkDatabase
metode dipanggil karena database pada saat itu belum ditemukan karena belum disalin dari aset (sebagaimana seharusnya).
Metode ini menggunakan getReadableDatabase
metode untuk mencoba melihat apakah database ada. Metode ini selalu mencetak stacktrace.
Pendekatan alternatif dapat menggantikan :-
private boolean checkDataBase(){
// this.getReadableDatabase();
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH ;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
dengan :-
private boolean checkDataBase(){
File db = new File(DB_PATH);
if(db.exists()) return true;
File dir = new File(db.getParent());
if (!dir.exists()) {
dir.mkdirs();
}
return false;
}
Ini mencoba untuk melihat apakah file database ada (dan juga membuat direktori yang tidak ada di jalur misalnya dalam beberapa keadaan direktori database mungkin tidak ada.)
Jika di atas digunakan maka pesan Anda akan lebih seperti :-
11-22 23:14:56.525 13193-13193/com.elytelabs.myapplication E/SQLiteLog: (1) no such table: Dictionary1
11-22 23:14:56.525 13193-13193/com.elytelabs.myapplication D/AndroidRuntime: Shutting down VM
11-22 23:14:56.525 13193-13193/com.elytelabs.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.elytelabs.myapplication, PID: 13193
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.elytelabs.myapplication/com.elytelabs.myapplication.MainActivity}: android.database.sqlite.SQLiteException: no such table: Dictionary1 (code 1): , while compiling: SELECT * FROM Dictionary1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1315)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5296)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
Caused by: android.database.sqlite.SQLiteException: no such table: Dictionary1 (code 1): , while compiling: SELECT * FROM Dictionary1
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
at com.elytelabs.myapplication.MainActivity.fetchData(MainActivity.java:142)
at com.elytelabs.myapplication.MainActivity.onCreate(MainActivity.java:82)