我的数据库正在运行,但我插入的所有记录都是为了检查目的,现在我想删除所有表并创建新表,所以我尝试通过更改版本来更新数据库。
我没有对创建表查询进行任何更改,但是我的外键约束失败(代码787)。
这是我的DBHelper
课程:
private static final String DATABASE_NAME = "roomatesDB";
// database 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";
// query 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);
}
答案 0 :(得分:2)
如果启用了外键约束,则在从数据库模式中删除表之前,DROP TABLE命令会执行隐式DELETE FROM命令。 [...]如果作为DROP TABLE命令的一部分执行的隐式DELETE FROM违反了任何直接外键约束,则会返回错误并且不会删除该表。
以正确的顺序删除数据,以便所有中间步骤都有效。或者只是禁用外部约束检查(db.setForeignKeyConstraintsEnabled()
)。