lundi 28 décembre 2015

SqliteException While Dropping Tables (main.table)

I've been using SQLite for a while in my application. I haven't had any issues, but this was my first upgrade. I incremented my DB version, and implemented onUpgrade as such:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < 2) {
        db.execSQL(CREATE_TABLE_LOCKED);
        Log.d(LOG_TAG, "Created table LOCKED_BUNCH_MEMBER");
        db.execSQL(CREATE_TABLE_HIDDEN);
        Log.d(LOG_TAG, "Created table HIDDEN_BUNCH_MEMBER");
    }
}

Everything seems to work fine! But on logout I drop all tables, and that's when I get a crazy weird error:

12-29 00:55:10.365: E/AndroidRuntime(1020): android.database.sqlite.SQLiteException: no such table: main.bunch (code 1): , while compiling: DROP TABLE IF EXISTS buddy

What the heck is this? I don't have a schema called main, and I've never seen this. This is the LAST drop table statement, and it fails. Anybody have an idea as to why?

Did I do the upgrade wrong?

public static LBDatabaseHelper getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new LBDatabaseHelper(context.getApplicationContext());
    }
    return mInstance;
}

private LBDatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.mContext = context;
}

@Override
public void onOpen(SQLiteDatabase db) {
    db.execSQL("PRAGMA foreign_keys=ON");
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("PRAGMA foreign_keys = ON");
    db.execSQL(CREATE_TABLE_BUDDY);
    db.execSQL(CREATE_TABLE_BUNCH);
    db.execSQL(CREATE_TABLE_BCAST_BUNCH);
    db.execSQL(CREATE_TABLE_BUNCH_MEMBER);
    db.execSQL(CREATE_TABLE_BCAST_BUNCH_MEMBER);
    db.execSQL(CREATE_TABLE_LOCKED_BUNCH_MEMBER);
    db.execSQL(CREATE_TABLE_HIDDEN_BUNCH_MEMBER);
    db.execSQL(CREATE_TABLE_THREAD);
    db.execSQL(CREATE_TABLE_THREAD_MEMBER);
    db.execSQL(CREATE_TABLE_THREAD_MESSAGE);
    db.execSQL(CREATE_TABLE_BUDDY_REQUEST);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < 2) {
        db.execSQL(CREATE_TABLE_LOCKED_BUNCH_MEMBER);
        Log.d(LOG_TAG, "Created table LOCKED_BUNCH_MEMBER");
        db.execSQL(CREATE_TABLE_HIDDEN_BUNCH_MEMBER);
        Log.d(LOG_TAG, "Created table HIDDEN_BUNCH_MEMBER");
    }
}

public void dropAllTables() {
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("DROP TABLE IF EXISTS " + THREAD_MESSAGE_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + THREAD_MEMBER_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + THREAD_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + LOCKED_BUNCH_MEMBER_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + HIDDEN_BUNCH_MEMBER_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + BCAST_BUNCH_MEMBER_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + BUNCH_MEMBER_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + BCAST_BUNCH_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + BUNCH_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + BUDDY_REQUEST_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + BUDDY_TABLE);
    onCreate(db);
    Log.d(LOG_TAG, "recreated tables");
}

Aucun commentaire:

Enregistrer un commentaire