我正在使用以下代码从资产中导入预填充(ORMLite)数据库。
除了Galaxy Note 10.1之外,这在多个设备上运行良好。在操作完成后关闭数据库时,我得到一个异常:
“错误代码= 11,msg =数据库损坏行....”
当我从设备下载数据库并在SqliteBrowser中打开它时,一切似乎都没问题。有什么想法吗?
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
// {....}
public void importDB() {
InputStream is = context.getAssets().open("DBName.db");
try {
close();
FileOutputStream os = new FileOutputStream(new File(Environment.getDataDirectory(), dbPath));
try {
byte[] buffer = new byte[4096];
for (int n; (n = is.read(buffer)) != -1;) {
os.write(buffer, 0, n);
}
} finally {
os.close();
}
} finally {
is.close();
}
getWritableDatabase().close();
}
错误:
I/SqliteDatabaseCpp(684): sqlite returned: error code = 11, msg = database corruption at line 48171 of [ed759d5a9e], db=/data/data/app_name/databases/DBName_db
I/SqliteDatabaseCpp(684): sqlite returned: error code = 11, msg = database disk image is malformed, db=/data/data/app_name/databases/DBName_db
E/SqliteDatabaseCpp(684): sqlite3_exec - Failed to set synchronous mode = 1(Normal)
I/SqliteDatabaseCpp(684): sqlite returned: error code = 11, msg = database corruption at line 48171 of [ed759d5a9e], db=/data/data/app_name/databases/DBName_db
I/SqliteDatabaseCpp(684): sqlite returned: error code = 11, msg = database disk image is malformed, db=/data/data/app_name/databases/DBName_db
E/SqliteDatabaseCpp(684): CREATE TABLE android_metadata failed
E/DefaultDatabaseErrorHandler(684): Corruption reported by sqlite on database: /data/data/app.name/databases/DBName.db
E/DefaultDatabaseErrorHandler(684): deleting the database file: /data/data/app.name/databases/DBName.db
答案 0 :(得分:0)
在关闭FileOutputStream之前,您始终必须同步:os.getFD().sync()
。这与文件系统有关。有些人在他们认为合适的时候会写 - 这可能会带来麻烦。
答案 1 :(得分:0)
正如您可以从logcat输出中看到的那样,您没有为DatabaseErrorHandler提供自定义实现,而隐式处理程序(DefaultDatabaseErrorHandler)只是删除损坏的数据库。您应该使用DefaultDatabaseErrorHandler
的实现你应该运行PRAGMA integrity_check;在恢复数据库上。这通常会返回字符串" ok",否则你可以解析字符串,例如,如果找到索引就可以删除并重新创建它。
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public MySQLiteOpenHelper(final Context context) {
super(context, DB_NAME, null, DATABASE_VERSION, new DatabaseErrorHandler() {
@Override
public void onCorruption(SQLiteDatabase db) {
//your code
}
});
}
// {....}
}