例外:在Android中,这是设置SQLiteDatabase助手的正确方法吗?
CREATE TABLE android_metadata failed
Failed to setLocale() when constructing, closing the database
android.database.sqlite.SQLiteException: database is locked
我的应用程序工作正常,并没有DB的问题,除了当onUpgrade()被调用。
当onUpgrade被自动调用时,它会尝试使用下面的CarManager类来完成升级所需的数据操作。这因为数据库被锁定而失败。
因为这似乎应该是做一个正常的事情,看来我不能构建正确下面的代码(两班跟随,辅助和一张桌子经理):
public class DbHelper extends SQLiteOpenHelper {
private Context context;
//Required constructor
public DbAdapter(Context context)
{
super(context, "my_db_name", null, NEWER_DB_VERSION);
this.context = context;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
overrideDB = db;
CarManager.migrateDataForOnUpgrade(context);
}
}
public class CarManager {
DbHelper dbHelper;
public CarManager(Context context)
{
dbHelper = new DbHelper(context);
}
public void addCar(String make, String model)
{
ContentValues contentValues = new ContentValues();
contentValues.put("make", make);
contentValues.put("model", model);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.insert("car", null, contentValues);
db.close();
}
public static void migrateDataForOnUpgrade()
{
//Code here that migrates data when onUpgrade() is called
//Db lock happens here
}
}
任何想法? 人们是否设置了不同于此的表管理器(例如:dao)?
编辑:我跟谷歌团队@ android开发者小时谈过,他们说onUpgrade3从来没有做过像结构变化(改变)的任何事情。所以,是的,现在好像有一些必须在许多情况下使用的黑客。
为了好奇,OrmLite(http://ormlite.com/)是设置DAO类的好方法。 – jcxavier 2012-03-02 18:13:36
发布例外 – Jack 2012-03-02 18:14:43
什么是例外? – drulabs 2012-03-02 18:16:52