Skip to content

Latest commit

 

History

History
51 lines (44 loc) · 2.83 KB

File metadata and controls

51 lines (44 loc) · 2.83 KB

Android Sqlite3 Corruption学习

  1. 接口DatabaseErrorHandler

  2. final 类DefaultDatabaseErrorHandler 调用SQLiteDatabase如下函数,配置DatabaseErrorHandler的实现,如果为null,默认采用DefaultDatabaseErrorHandler

SQLiteDatabase openOrCreateDatabase(String, android.database.sqlite.SQLiteDatabase.CursorFactory, DatabaseErrorHandler)
SQLiteDatabase openDatabase(String, android.database.sqlite.SQLiteDatabase.CursorFactory, int, DatabaseErrorHandler)
  1. 异常SQLiteDatabaseCorruptException 标识sqlite3数据库文件污染

  2. 应用内需要捕获SQLiteDatabaseCorruptException实现自定义行为 定义DatabaseErrorHandler 子类;
    定义SQLiteOpenHelper子类,调用父类构造函数传递自定义DatabaseErrorHandler
    自定义DatabaseErrorHandler 子类,参考DefaultDatabaseErrorHandler源码

  3. 导致数据库文件corrupt原因,参考

  • File overwrite by a rogue thread or process

a) Continuing to use a file descriptor after it has been closed; b) Backup or restore while a transaction is active; c) Deleting a hot journal;

  • File locking problems

a) Filesystems with broken or missing lock implementations; b) Posix advisory locks canceled by a separate thread doing close(); c) Two processes using different locking protocols; d) Unlinking or renaming a database file while in use; e) Multiple links to the same file;

  • Failure to sync

a) Disk drives that do not honor sync requests; b) Disabling sync using PRAGMAs;

  • Disk Drive and Flash Memory Failures

Non-powersafe flash memory controllers; Fake capacity USB sticks;

  • Memory corruption
  • Other operating system problems
  • Bugs in SQLite
  1. 对于API大于等于11、API小于11的差别 API小于11,压根没有DatabaseErrorHandler一说!! API大于等于11,SQLiteOpenHelper源码
    API大于等于11,SQLiteDatabase源码
    API小于11,SQLiteDatabase源码