我有一个基于SQLiteOpenHelper的现有数据库,它有几个版本和代码来升级它,并且工作正常.但是,如果用户安装了较旧版本的应用程序(需要较低的数据库版本),它将会崩溃 – 使用它的ContentProvider无法访问数据库.我想防止它崩溃,但我不想实际降级数据库 – 添加代码来做这件事会很痛苦.删除所有表肯定会起作用,但从一个新文件开始是更清洁,更不容易出错.
这是关于数据库助手的样子 – 没什么特别的
public class MyDbHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 3; private static final String DATABASE_NAME = "my.db"; public MyDbHelper(Context context) { super(context,DATABASE_NAME,null,DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { onUpgrade(db,DATABASE_VERSION); } @Override public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) { if (newVersion < 1) db.execSQL("CREATE TABLE A..."); if (newVersion < 2) db.execSQL("CREATE TABLE B..."); if (newVersion < 3) db.execSQL("CREATE TABLE C..."); } @Override public void onDowngrade(SQLiteDatabase db,int newVersion) { // I'd like to delete the database here // the only problem is that I can't from here // since this is called in the middle of getWritableDatabase() // and SQLiteDatabase has no .recreate() method. } }
我可能采取的方法是:
>从外部执行:捕获ContentProvider中的异常,删除文件并请求再次打开数据库. – 我不喜欢这样,因为它不是提供者的责任.
>用我自己的那个删除文件而不是调用onDowngrade的类的副本替换SQLiteOpenHelper – 问题是它使用的是SQLiteDatabase的包私有部分(例如.lock()),如果不复制SQLiteDatabase我也无法替换它(可能会导致重复整个sqlite堆栈).
有没有什么好方法可以做到这一点,或者我必须采用DROP TABLES方式,例如喜欢描述here?
解决方法
我已经找到了一种通过扩展SQLiteOpenHelper很好地工作的方法,我在MyDbHelper中需要做的就是扩展这个类.
public abstract class DeletingSQLiteOpenHelper extends SQLiteOpenHelper { private static final String TAG = DeletingSQLiteOpenHelper.class.getSimpleName(); private final File mDatabaseFile; public DeletingSQLiteOpenHelper(Context context,String name,CursorFactory factory,int version,DatabaseErrorHandler errorHandler) { super(context,name,factory,version,errorHandler); mDatabaseFile = context.getDatabasePath(name); } public DeletingSQLiteOpenHelper(Context context,int version) { super(context,version); mDatabaseFile = context.getDatabasePath(name); } @Override public synchronized SQLiteDatabase getWritableDatabase() { try { return super.getWritableDatabase(); } catch (SQLiteDowngradeFailedException e) { // that's our notification } // try to delete the file mDatabaseFile.delete() // now return a freshly created database return super.getWritableDatabase(); } @Override public final void onDowngrade(SQLiteDatabase db,int newVersion) { // throwing a custom Exception to catch it in getWritableDatabase throw new SQLiteDowngradeFailedException(); } // that's the exception static class SQLiteDowngradeFailedException extends SQLiteException { public SQLiteDowngradeFailedException() {} public SQLiteDowngradeFailedException(String error) { super(error); } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。