我面临当前的问题:
我有一个带有第一个 ActivityMainActivity
和第二个 Activity的应用程序SecondActivity
。现在,在这两个中我都需要一个单独的表:
这是我的 HelperClass:
public class FMSDatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String FMS_TABLE_CREATE = "CREATE TABLE fms (fmsId integer primary key autoincrement, status TEXT, date TEXT, kennung TEXT);";
private static final String ZVEI_TABLE_CREATE = "CREATE TABLE zvei (zveiId integer primary key autoincrement, schleife TEXT, date TEXT, description TEXT);";
public static final String TABLE_FMS = "fms";
public static final String TABLE_ZVEI = "zvei";
public static final String COLUMN_FMS_ID = "fmsId";
public static final String COLUMN_FMS_KENNUNG = "kennung";
public static final String COLUMN_FMS_STATUS = "status";
public static final String COLUMN_FMS_DATE = "date";
public static final String COLUMN_ZVEI_ID = "zveiId";
public static final String COLUMN_ZVEI_SCHLEIFE = "schleife";
public static final String COLUMN_ZVEI_DATE = "date";
public static final String COLUMN_ZVEI_DESCRIPTION = "description";
// ----------------------------------------------------------------------
public FMSDatabaseHandler(Context context) {
super(context, "fms.db", null, DATABASE_VERSION);
}
// ----------------------------------------------------------------------
@Override public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(FMS_TABLE_CREATE + ZVEI_TABLE_CREATE);
//db.execSQL(FMS_TABLE_CREATE);
//db.execSQL(ZVEI_TABLE_CREATE);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// ----------------------------------------------------------------------
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FMS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ZVEI);
onCreate(db);
}
// ----------------------------------------------------------------------
public List<ZVEI> getAllZvei() {
List<ZVEI> zveiList = new ArrayList<ZVEI>();
String selectQuery = "SELECT * FROM " + TABLE_ZVEI + " ORDER BY zveiId DESC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
ZVEI zvei = new ZVEI();
zvei.setZveiId(Integer.parseInt(cursor.getString(0)));
zvei.setSchleife(cursor.getString(1));
zvei.setDate(cursor.getString(2));
zvei.setDescription(cursor.getString(3));
zveiList.add(zvei);
} while (cursor.moveToNext());
}
return zveiList;
}
}
MainActivity
开始时,该方法getAllZvei()
被调用,所以在我看来,HelperClass 中的onCreate()
方法(通过db.getWritableDatabase()
)也应该被调用。神秘:表zvei
已创建,但不是fms
. 所以当我开始我的第二个活动时(它调用另一个方法,getAllFms()
类似于getAllZvei()
,刚刚在此源中将其删除),它崩溃并且 LogCat 告诉我:
noch such table: fms
我在 Nexus 4 模拟器上运行它。擦除用户数据,从应用程序中清除数据并卸载应用程序。我fms.db
通过 DDMS 提取并在 SQLiteBrowser 中查看,但只有 zvei 存在。
我无法解释为什么。你能?
更新 我刚刚重命名了数据库名称并在 DDMS 中识别,仍然生成了旧名称。所以我只是清理并重建了项目,删除了模拟器并创建了一个新的,擦除了用户数据(是的,即使我刚刚创建了它!)现在:它可以工作了!
感谢大家!