Home > Mobile >  Android DBopenHelper class implements DBopenHelper method is to use absolute paths, the app won'
Android DBopenHelper class implements DBopenHelper method is to use absolute paths, the app won'

Time:09-25

2020-01-01 23:58:29. 869, 17144-17144/com. Example. Myapplication E/AndroidRuntime: FATAL EXCEPTION: the main
Process: com. Example. Myapplication, PID: 17144
Java. Lang. RuntimeException: Unable to start the activity ComponentInfo {com. Example. Myapplication/com. Example. Myapplication. LoginActivity} : android. Database. Sqlite. SQLiteCantOpenDatabaseException: unknown error code (14) : Could not open the database
. An android app. ActivityThread. PerformLaunchActivity (ActivityThread. Java: 2646)
. An android app. ActivityThread. HandleLaunchActivity (ActivityThread. Java: 2707)
. An android app. ActivityThread. - wrap12 (ActivityThread. Java)
An android. App. ActivityThread $H.h andleMessage (ActivityThread. Java: 1460)
An android. OS. Handler. DispatchMessage (Handler. Java: 102)
. An android OS. Stars. Loop (154). Which Java:
. An android app. ActivityThread. Main (6077) ActivityThread. Java:
The at Java. Lang. Reflect. Method. Invoke (Native Method)
At com. Android. Internal. OS. $MethodAndArgsCaller ZygoteInit. Run (ZygoteInit. Java: 865)
At com. Android. Internal. OS. ZygoteInit. Main (ZygoteInit. Java: 755)
Under Caused by: android. Database. Sqlite. SQLiteCantOpenDatabaseException: unknown error code (14) : Could not open the database
An android. Database. Sqlite. SQLiteConnection. NativeOpen (Native Method)
An android. Database. Sqlite. SQLiteConnection. Open (209) SQLiteConnection. Java:
An android. Database. Sqlite. SQLiteConnection. Open (193) SQLiteConnection. Java:
An android. Database. Sqlite. SQLiteConnectionPool. OpenConnectionLocked (SQLiteConnectionPool. Java: 463)
An android. Database. Sqlite. SQLiteConnectionPool. Open (185) SQLiteConnectionPool. Java:
An android. Database. Sqlite. SQLiteConnectionPool. Open (177) SQLiteConnectionPool. Java:
An android. Database. Sqlite. SQLiteDatabase. OpenInner (SQLiteDatabase. Java: 808)
An android. Database. Sqlite. SQLiteDatabase. Open (SQLiteDatabase. Java: 793)
An android. Database. Sqlite. SQLiteDatabase. OpenDatabase (SQLiteDatabase. Java: 696)
. An android app. ContextImpl. OpenOrCreateDatabase (ContextImpl. Java: 680)
An android. Content. ContextWrapper. OpenOrCreateDatabase (ContextWrapper. Java: 289)
An android. Database. Sqlite. SQLiteOpenHelper. GetDatabaseLocked (SQLiteOpenHelper. Java: 223)
An android. Database. Sqlite. SQLiteOpenHelper. GetWritableDatabase (SQLiteOpenHelper. Java: 163)
At com. Example. Myapplication. DBdao. & lt; init> (DBdao. Java: 14)
At com. Example. Myapplication. LoginActivity. OnCreate (loginActivity. Java: 57)
. An android app. Activity. PerformCreate (6664) Activity. Java:
. An android app. Instrumentation. CallActivityOnCreate (1118) Instrumentation. Java:
. An android app. ActivityThread. PerformLaunchActivity (ActivityThread. Java: 2599)
. An android app. ActivityThread. HandleLaunchActivity (ActivityThread. Java: 2707)
. An android app. ActivityThread. - wrap12 (ActivityThread. Java)
An android. App. ActivityThread $H.h andleMessage (ActivityThread. Java: 1460)
An android. OS. Handler. DispatchMessage (Handler. Java: 102)
. An android OS. Stars. Loop (154). Which Java:
. An android app. ActivityThread. Main (6077) ActivityThread. Java:
The at Java. Lang. Reflect. Method. Invoke (Native Method)
At com. Android. Internal. OS. $MethodAndArgsCaller ZygoteInit. Run (ZygoteInit. Java: 865)
At com. Android. Internal. OS. ZygoteInit. Main (ZygoteInit. Java: 755)

CodePudding user response:

DBopenHelper data in name only the relative path, but you can customize a ContextWrapper to handle your path,
 
Public class DatabaseContext extends ContextWrapper {
Public DatabaseContext Context (base) {
Super (base);
}

@ Override
The public File getDatabasePath (String name) {
If (name. StartsWith (Environment. External.getexternalstoragedirectory () getAbsolutePath ())) {
//begin with sd card, your processing
} else {
//your processing
}
Return super. GetDatabasePath (name);
}

@ Override
Public SQLiteDatabase openOrCreateDatabase (String name, int mode,
SQLiteDatabase. CursorFactory factory,
DatabaseErrorHandler errorHandler) {
Return openOrCreateDatabase (name, mode, the factory);
}

@ Override
Public SQLiteDatabase openOrCreateDatabase (String name, int mode,
SQLiteDatabase. CursorFactory factory) {
Return SQLiteDatabase. OpenOrCreateDatabase (
GetDatabasePath (name), null);
}
}

Custom Sqlite nHelper, take the context into custom
 
*/
Public class CustomDbHelper extends SQLiteOpenHelper {
Public CustomDbHelper (Context Context, String name, SQLiteDatabase. CursorFactory factory, int version) {
Super (new DatabaseContext (context), name, factory, version);
}
.
}

It is important to note that if you use the sd card database, memory card should be well dealt with
  • Related