Error when trying to update sqlite database in android

心已入冬 提交于 2019-12-20 07:27:25

问题


I had my database 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 tried updating the database by changing the version.

I didn't change anything on the create table queries but I'm getting a foreign key constraint failed (code 787).

Here is my DBHelper class:

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);

    }

回答1:


The documentation says:

If foreign key constraints are enabled, a DROP TABLE command performs an implicit DELETE FROM command before removing the table from the database schema. […] If the implicit DELETE FROM executed as part of a DROP TABLE command violates any immediate foreign key constraints, an error is returned and the table is not dropped.

Remove the data in the correct order so that all intermediate steps are valid. Or just disable foreign constraint checking (db.setForeignKeyConstraintsEnabled()).



来源:https://stackoverflow.com/questions/36766061/error-when-trying-to-update-sqlite-database-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!