i had my data base working but all the records i have inserted were for checking purpose, now i wanted to delete all the tables and creating new ones, so i tryed updating the data base but changing the version. i didnt changed nothing on the create table querys but im getting the foreign key constraint failed (code 787). here is my DBHElper class:
private static final String DATABASE_NAME = "roomatesDB";
//data base version
private static final int DATABASE_VERSION = 3;
//tables name
public static final String APARTMENT_TABLE = "apartment";
public static final String ROOMATE_TABLE = "roomate";
public static final String SHOPCART_TABLE = "shopcart";
public static final String ITEMS_TABLE = "items";
//common column
public static final String APARTMENT_NUMBER_COLUMN = "apartmentNum";
//roomates table columns
public static final String FIRST_NAME_COLUMN = "firstName";
public static final String LAST_NAME_COLUMN = "lastName";
public static final String PHONE_NUMBER_COLUMN = "phoneNumber";
//shop cart table columns
public static final String NUMBER_COLUMN = "number";
public static final String LIST_NAME_COLUMN = "name";
//item table
public static final String PRICE_COLUMN = "price";
public static final String ITEM_NAME_COLUMN = "name";
//qeury for creating roomate table
public static final String CREATE_ROOMATE_TABLE = "CREATE TABLE "
+ ROOMATE_TABLE + "(" + FIRST_NAME_COLUMN + " TEXT, "
+ LAST_NAME_COLUMN + " TEXT, " + PHONE_NUMBER_COLUMN + " TEXT, "
+ APARTMENT_NUMBER_COLUMN + " INTEGER, "
+ "FOREIGN KEY(" + APARTMENT_NUMBER_COLUMN + ") REFERENCES "
+ APARTMENT_TABLE + "(apartmentNum) " + ")";
//query for crating shop cart table
public static final String CREATE_SHOPLIST_TABLE = "CREATE TABLE "
+ SHOPCART_TABLE + "(" + NUMBER_COLUMN + " INTEGER PRIMARY KEY,"
+ LIST_NAME_COLUMN + " TEXT, "
+ APARTMENT_NUMBER_COLUMN + " INTEGER, "
+ "FOREIGN KEY(" + APARTMENT_NUMBER_COLUMN + ") REFERENCES "
+ APARTMENT_TABLE + "(apartmentNum) " + ")";
//query for creating shop item table
public static final String CREATE_SHOPITEM_TABLE = "CREATE TABLE "
+ ITEMS_TABLE + "(" + ITEM_NAME_COLUMN + " TEXT,"
+ PRICE_COLUMN + " DOUBLE, "
+ NUMBER_COLUMN + " INT, "
+ "FOREIGN KEY(" + NUMBER_COLUMN + ") REFERENCES "
+ SHOPCART_TABLE + "(number) " + ")";
//query for creating apartment table
public static final String CREATE_APARTMENT_TABLE = "CREATE TABLE "
+ APARTMENT_TABLE + "(" + APARTMENT_NUMBER_COLUMN + " INTEGER PRIMARY KEY"
+ ")";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
db.setForeignKeyConstraintsEnabled(true);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_APARTMENT_TABLE);
db.execSQL(CREATE_ROOMATE_TABLE);
db.execSQL(CREATE_SHOPLIST_TABLE);
db.execSQL(CREATE_SHOPITEM_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + APARTMENT_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + ROOMATE_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + SHOPCART_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + ITEMS_TABLE);
onCreate(db);
}
Aucun commentaire:
Enregistrer un commentaire