长期以来,我一直对这个问题感到困扰。我的db文件中也有android_metadata 首先,我从资产中复制并解压缩数据库,这里没有错误。但是当我检查DBValid时,会发生错误。不是在所有设备上。只有少数设备。
这是我的代码:
private void copyAndUnzipDB() {
File dbFile;
if (checkInnerAvailable()){
dbFile = getDbFileInInnerStorage();
}else if (checkSDSpaceAvailable()){
dbFile = getDbFileInSDCard();
}else {
throw new NoFreeSpaceException("no free space");
}
FileOutputStream fos = null;
InputStream is = null;
try {
is = mContext.getAssets().open(DB_ZIP_NAME);;
ZipInputStream zis = AssetUtil.getFileFromZip(is);
if (zis == null) {
throw new RuntimeException("Archive is missing a SQLite database file");
}
fos = new FileOutputStream(dbFile);
IOUtils.copy(zis, fos);
fos.flush();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("unzip failed", e);
}finally {
IOUtils.closeQuietly(fos);
IOUtils.closeQuietly(is);
}
checkDbValid(dbFilePath);
}
private static boolean checkDbValid(String dbPath){
SQLiteDatabase checkDB = null;
Cursor cursor = null;
try {
checkDB = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY, new DatabaseErrorHandler(){
@Override
public void onCorruption(SQLiteDatabase dbObj) {
isValid = false;
}
});
cursor = checkDB.rawQuery("select DISTINCT tbl_name from sqlite_master", null);
if (cursor.getCount() > 2){
isValid = true;
return isValid;
}
} catch (Throwable e) {
// database doesn't exist yet.
e.printStackTrace();
}finally {
if (cursor != null){
cursor.close();
}
DBTools.closeDatabaseQuietly(checkDB);
}
return false;
}
错误日志在这里:
09-21 23:09:49.294 26017 26109 E SQLiteLog: (11) database corruption at line 57173 of [b3bb660af9]
4109-21 23:09:49.294 26017 26109 E SQLiteLog: (11) database disk image is malformed
4209-21 23:09:49.294 26017 26109 W System.err: android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed (code 11): , while compiling: select DISTINCT tbl_name from sqlite_master
4309-21 23:09:49.294 26017 26109 W System.err: #################################################################
4409-21 23:09:49.294 26017 26109 W System.err: Error Code : 11 (SQLITE_CORRUPT)
4509-21 23:09:49.294 26017 26109 W System.err: Caused By : The database disk image is malformed.
4609-21 23:09:49.294 26017 26109 W System.err: (database disk image is malformed (code 11): , while compiling: select DISTINCT tbl_name from sqlite_master)
4709-21 23:09:49.294 26017 26109 W System.err: #################################################################
4809-21 23:09:49.304 26017 26109 W System.err: at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
4909-21 23:09:49.304 26017 26109 W System.err: at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
5009-21 23:09:49.304 26017 26109 W System.err: at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
5109-21 23:09:49.304 26017 26109 W System.err: at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
5209-21 23:09:49.304 26017 26109 W System.err: at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
5309-21 23:09:49.304 26017 26109 W System.err: at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
5409-21 23:09:49.304 26017 26109 W System.err: at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
5509-21 23:09:49.304 26017 26109 W System.err: at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1454)