lundi 14 mars 2016

How to replace a table in SQLite

I'm trying to do a database migration where I remove a table and add a unique restriction on one of the columns inside of my Android application.

I created the new table, copied the values from the original values to the new table, dropped the old table, and then renamed the new table to the same name as the old table.

Problem is that now when I query the database for the values in the original table name, I'm getting results from the allegedly dropped table instead of the new table that was renamed.

I've tried doing everything except renaming the new table in order to verify everything was hooked up correctly. I see the new table with the correct columns. But querying the old table gives me valid responses instead of an error for "table does not exist".

This is happening inside of a test where I am setting up the database, calling onUpgrade, and then querying the state.

To reiterate:

  1. Create table A with columns 1, 2, 3
  2. Create table B with columns 1, 2, 4, 5
  3. Copy rows from table A to table B, dropping column 3 and having default values for columns 4 and 5.
  4. Drop table A
  5. Rename table B to table A
  6. Get entries with columns 1, 2, 3 when querying table A

Alternate:

  • Skip step 5
  • Can still query table A and table B

Do I need to perform some sort of flush to get the dropped table to actually drop?

/* Create table_a */
/* Populate table_a */
/* Create table_b */
/* Copy table_a contents to table_b */
db.execSQL("DROP TABLE IF EXISTS table_a");
db.execSQL("ALTER TABLE table_b RENAME TO table_a");

Thanks.

Aucun commentaire:

Enregistrer un commentaire