Android 应用程序崩溃后数据库加载为空
我的SQLite数据库遇到了一个奇怪的问题。当我第一次启动应用程序时实例化我的Android 应用程序崩溃后数据库加载为空,android,sqlite,sqliteopenhelper,Android,Sqlite,Sqliteopenhelper,我的SQLite数据库遇到了一个奇怪的问题。当我第一次启动应用程序时实例化我的OpenHelper时,我的数据库正常加载,我可以检索所有数据。如果应用程序崩溃,android manager会在我的第一个活动中重新弹出我的应用程序。因此,所有的初始化过程都是相同的,但由于某些原因,加载数据库时没有任何数据 下面是我的OpenHelper构造函数: public static AppDatabaseHelper getInstance(Context ctx, Handler uiHandl
OpenHelper
时,我的数据库正常加载,我可以检索所有数据。如果应用程序崩溃,android manager会在我的第一个活动中重新弹出我的应用程序。因此,所有的初始化过程都是相同的,但由于某些原因,加载数据库时没有任何数据
下面是我的OpenHelper
构造函数:
public static AppDatabaseHelper getInstance(Context ctx, Handler uiHandler, String companyName){
if(mInstanceWithHandler==null){
DATABASENAME=companyName;
mInstanceWithHandler=new AppDatabaseHelper(ctx);
}
return mInstanceWithHandler;
}
private AppDatabaseHelper(Context context) {
super(context, DATABASENAME, null, DATABASE_VERSION);
this.context = context;
}
和我的数据源
public AppDataSource(Context context, Handler handler, String companyName) {
dbHelper = AppDatabaseHelper.getInstance(context, handler, companyName);
database = dbHelper.getWritableDatabase();
this.context = context;
}
我真的能弄明白为什么在应用程序崩溃后加载了相同的数据库,但没有任何数据。它是由系统层处理的吗
编辑:onCreate/onUpgrade
@Override
public void onCreate(SQLiteDatabase db) {
// build new tables
db.execSQL(createSupplier(SUPPLIERS_TABLENAME));
db.execSQL(createProduct(PRODUCTS_TABLENAME));
db.execSQL(createUser(USERS_TABLENAME));
....
}
@Override
public void onUpgrade(final SQLiteDatabase db, int oldVersion,
int newVersion) {
// Create new tables with temp name
db.execSQL(createSupplier(SUPPLIERS_TABLENAME + "_temp"));
....
// insert data from old to new tables
Cursor cursor = db.rawQuery(
"select tbl_name from sqlite_master ", null);
List<String> tables = new ArrayList<String>();
if (cursor.getCount() > 0)
while (cursor.moveToNext())
....
// rename new tables
db.execSQL("alter table "
+ AppDatabaseHelper.SUPPLIERS_TABLENAME + "_temp"
+ " rename to " + SUPPLIERS_TABLENAME);
....
}
@覆盖
public void onCreate(SQLiteDatabase db){
//建立新表
db.execSQL(createSupplier(SUPPLIERS_TABLENAME));
db.execSQL(createProduct(PRODUCTS_TABLENAME));
db.execSQL(createUser(USERS_TABLENAME));
....
}
@凌驾
public void onUpgrade(最终SQLiteDatabase db,intoldversion,
int(新版本){
//使用临时名称创建新表
db.execSQL(createSupplier(SUPPLIERS_TABLENAME+“_temp”);
....
//将数据从旧表插入新表
Cursor=db.rawQuery(
“从sqlite_master中选择tbl_名称”,空);
列表表=新的ArrayList();
if(cursor.getCount()>0)
while(cursor.moveToNext())
....
//重命名新表
db.execSQL(“altertable”
+AppDatabaseHelper.SUPPLIERS\u TABLENAME+“\u temp”
+“重命名为”+供应商名称);
....
}
发布stacktrace和sqlite open helperonCreate()
/onUpgrade()
回调。不幸的是没有stacktrace(没有错误)数据库正常加载,我甚至可以在上面保存数据,但如果我强制应用程序停止(从任务管理器),那么我会丢失数据(应该在崩溃后保存)我会找回我的正常数据…那么你在哪里以及如何保存丢失的数据呢?