0

I am working with Android and ORMLite. I am newbie and I upgrading the database to the Version 2. I have the class “Auto” and need to drop the column Alias.

    @DatabaseTable(tableName = "Auto")
public class Auto {

    public static final String ALIAS                 = "alias";
    public static final String PLACA                 = "placa";


    @DatabaseField(generatedId = true, columnName = "ID")
    private int id;
    @DatabaseField(canBeNull = false, columnName = ALIAS)
    private String alias;
    @DatabaseField(canBeNull = false, columnName = PLACA)

    public Auto()
    {
        //ORMLite needs a no-arg constructor
    }

    public Auto(String alias, String placa) {
        this.alias                  = alias;
        this.placa                  = placa;
    }

…..
…..
}

I change the class Auto to…

@DatabaseTable(tableName = "Auto")
public class Auto {

    public static final String PLACA                 = "placa";


    @DatabaseField(generatedId = true, columnName = "ID")
    private int id;
    @DatabaseField(canBeNull = false, columnName = PLACA)

    public Auto()
    {
        //ORMLite needs a no-arg constructor
    }

    public Auto(String alias, String placa) {       this.placa                  = placa;
    }

…..
…..
}

I need to know if ORMLite automatically drops the column Alias when execute “onUpgradeMethod” or I have to do manually like this.

@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
   if(oldVersion == 1) {
   try {
      Dao dao;
      dao = getAutoDao();
      dao.executeRaw("ALTER TABLE `Auto` DROP COLUMN alias;");

   } catch (SQLException e) {
      Log.e(DatabaseHelper.class.getName(), "Error ", e);

   }
   }
}
AfroChase
  • 178
  • 2
  • 14

1 Answers1

1

This links help me to solve it.

Drop column in SQLite

FAQ SQLite 11

Delete column from SQL

save data before table upgrade

Drop table if it already exists and then re-create it?

SQLite has limited ALTER TABLE support that you can use to add a column to the end of a table or to change the name of a table. If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.

For example, suppose you have a table named "t1" with columns names "a", "b", and "c" and that you want to delete column "c" from this table. The following steps illustrate how this could be done:

BEGIN TRANSACTION; 
CREATE TEMPORARY TABLE t1_backup(a,b);
INSERT INTO t1_backup SELECT a,b FROM t1; 
DROP TABLE t1; CREATE TABLE t1(a,b); INSERT INTO t1 SELECT a,b FROM t1_backup;
DROP TABLE t1_backup; 
COMMIT;

The possible solution according to Phil's comments is this. 4

So your choices are:

  1. leave things as they are,

  2. add a new column, copying all the data from old to new as part of the upgrade, and just ignore the old column altogether, or

  3. Use a drop/create strategy to upgrade: back up the data from table into a temporary table, drop the table, re-create it as you'd like it to be, copy all the data back into it, and finally drop the temporary table.

Database upgrades are always nervous affairs (need lots of error handling, lots of testing), frankly if the name is the only thing that concerns you, I'd leave it alone (option 1). If you really need to change it then option 2 is low risk but leaves a 'dead' column and data lying around. Option 3 may be your choice if, for example, the data is a significant percentage of the overall database size.

I decided to delete the data from the column but don't drop the column, that is similar to solution 2.

Community
  • 1
  • 1
AfroChase
  • 178
  • 2
  • 14