Jeg mener, at databasen faktisk er blevet kopieret og indlæst (åbnet) med succes, og snarere at problemet er, at den kopierede database ikke har en tabel ved navn Dictionary1 når du forsøger følgende:-
Cursor cursor = sd.query("Dictionary1" ,null, null, null, null, null, null);
I henhold til at com.elytelabs.myapplication.MainActivity.fetchData(MainActivity.java:142)
Så du skal rette tabelnavnet til at være i overensstemmelse med tabellen i den kopierede database .
Du ønsker måske at gøre brug af denne Q&A til at vise databaseoplysninger.
Beskederne :-
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 .........
resultat fra, når checkDatabase
metoden påberåbes, da databasen ikke på det tidspunkt er blevet fundet, da den ikke er blevet kopieret over fra aktiverne (som den burde gøre).
Metoden bruger getReadableDatabase
metode til at prøve at se, om databasen eksisterer. Denne metode udskriver altid stacktrace.
En alternativ tilgang kunne være at erstatte :-
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;
}
med :-
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;
}
Dette forsøger at se, om databasefilen eksisterer (og opretter også en ikke-eksisterende mappe i stien, f.eks. i nogle tilfælde eksisterer databasemappen muligvis ikke.)
Hvis ovenstående bruges, er dine beskeder mere som :-
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)