예외 :Android에서 SQLiteDatabase 헬퍼를 설정하는 올바른 방법은 무엇입니까?
CREATE TABLE android_metadata failed
Failed to setLocale() when constructing, closing the database
android.database.sqlite.SQLiteException: database is locked
내 애플 onUpgrade()가 호출되는 경우를 제외하고는 잘 작동하고 더 DB 문제가 없습니다.
onUpgrade가 자동으로 호출되면 아래의 CarManager 클래스를 사용하여 업그레이드에 필요한 데이터 조작을 시도합니다. db가 잠겨 있기 때문에 실패합니다.
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)를 다르게 설정합니까?
편집 : Google 팀 @ Android 개발자 팀과 이야기를 나누었습니다. onUpgrade3은 결코 구조 변경 (변경)과 같은 것을 할 의도가 없었습니다. 그래서 예, 많은 예제에서 사용해야하는 해킹이있는 것처럼 보입니다.
호기심을 위해서 OrmLite (http://ormlite.com/)는 DAO 클래스를 설정하는 좋은 방법입니다. – jcxavier
예외 게시 – Jack
예외는 무엇입니까? – drulabs