我想修復一個不允許我打開數據庫的錯誤。可能通過所有的合併,出現了問題,但我似乎無法找到它。這是我的Log Cat
報告。android.database.sqlite.SQLiteCantOpenDatabaseException:未知錯誤(代碼14):無法打開數據庫故障
E/SQLiteLog(894): (14) cannot open file at line 30191 of [00bb9c9ce4]
E/SQLiteLog(894): (14) os_unix.c:30191: (2) open(/data/data/com.packagename/databases/MainDB) -
Failed to open database '/data/data/com.packagename/databases/MainDB'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
at com.packagename.database.DBAdapter$DatabaseHelper.checkDataBase(DBAdapter.java:757)
at com.packagename.database.DBAdapter$DatabaseHelper.createDataBase(DBAdapter.java:734)
at com.packagename.database.DBAdapter.resetAllData(DBAdapter.java:490)
at com.packagename.OptionActivity$4.onClick(OptionActivity.java:157)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
這裏是可見的(或相關的)代碼的片段回到日誌。
選項活動
public void onClick_ResetAll(final View v) {
....
....
@Override
public void onClick(DialogInterface dialog, int which) {
TrackUtils.sendClickTrack(OptionActivity.this, "ui_action", "button_press", "Resetall_yes");
final DBAdapter dba = new DBAdapter(v.getContext());
dba.open();
dba.resetAllData(v.getContext());
dba.close();
}
....
....
}
將對DBAdapter類
private DatabaseHelper mDbHelper;
private static SQLiteDatabase mDb;
@SuppressLint("SdCardPath")
private static String DB_PATH;
private static final String DATABASE_NAME = "MainDB";
private static final int DATABASE_VERSION = 3;
private final Context adapterContext;
public DBAdapter(Context context) {
this.adapterContext = context;
DB_PATH = adapterContext.getFilesDir().getAbsolutePath().replace("files", "databases")
+ File.separator;
}
public DBAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(adapterContext);
try {
mDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
mDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
return this;
}
public void resetAllData(Context context) {
context.deleteDatabase("MainDB");
mDbHelper = new DatabaseHelper(adapterContext);
try {
mDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
mDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
}
DatabaseHelper(延伸SQLiteOpenHelper)
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
helperContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY); // This is where the error apparently occurs
} catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
我已經在本網站上搜索解決方案,但無濟於事。
考慮使用古老的博客文章的破變種https://github.com/jgilfelt/android-sqlite-asset-helper代替。 – laalto 2014-10-01 06:22:13