I'm facing the current problem:
I have an application with a first Activity MainActivity
and a second Activity called SecondActivity
.
Now, in both of these I need a separate table:
This is my 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;
}
}
When the MainActivity
starts, the getAllZvei()
method ist called, so in my opinion, the onCreate()
method (via the db.getWritableDatabase()
) from the HelperClass should be also called.
Mysterious: the table zvei
is created, but NOT the fms
. So when I start my second activity (which calls another method, getAllFms()
, similar to getAllZvei()
, just removed it in this source here), it crashes and LogCat tells me:
noch such table: fms
I run this on a Nexus 4 emulator. Wiped the user data, cleared data from the app and uninstalled the app. I pulled the fms.db
via DDMS and viewed in SQLiteBrowser, but only zvei is present.
I can't explain why. Can you?
UPDATE I just renamed the database name and recognized in DDMS, that still the old name was generated. So I just cleaned and rebuilt the project, deleted the emulator and created a new one, wiped the user data (yes, even if I just created it!) and now: it works!
Thanks to all of you!