在ORMLite中更改架构时删除列 [英] Drop a column when the schema changes in ORMLite

查看:37
本文介绍了在ORMLite中更改架构时删除列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android和ORMLite.我是新手,我将数据库升级到版本2.我有自动"类,需要删除别名"列.

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

…..
…..
}

我需要知道ORMLite在执行"onUpgradeMethod"时是否会自动删除别名"列,还是我必须像这样手动进行.

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

   }
   }
}

推荐答案

此链接可帮助我解决问题.

This links help me to solve it.

在SQLite中删除列

常见问题解答SQLite 11

从SQL删除列

在表升级之前保存数据

如果已删除表存在,然后重新创建?

SQLite具有有限的ALTER TABLE支持,可用于将列添加到表的末尾或更改表的名称.如果要对表的结构进行更复杂的更改,则必须重新创建表.您可以将现有数据保存到临时表中,删除旧表,创建新表,然后将数据从临时表中复制回去.

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.

例如,假设您有一个名为"t1"的表,该表的列名称为"a","b"和"c",并且您要从中删除"c"列桌子.以下步骤说明了如何完成此操作:

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;

根据菲尔的评论,可能的解决方案是这样. 4

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

因此,您的选择是:

So your choices are:

  1. 保持现状

  1. leave things as they are,

添加新列,作为升级的一部分,将所有数据从旧数据复制到新数据,而完全忽略旧列,或者

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

使用删除/创建策略进行升级:将表中的数据备份到临时表中,删除表,然后根据需要重新创建将所有数据复制回去,最后删除临时数据表.

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.

数据库升级始终是一件令人紧张的事情(需要大量错误处理,大量测试),坦率地说,如果名称是唯一的关于您的问题,我将不理会(选项1).如果您真的需要进行更改,然后选项2处于低风险状态,但留有死"列,并且数据随处可见.例如,如果数据占整个数据库大小的很大百分比.

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.

我决定从列中删除数据,但不删除列,这与解决方案2类似.

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

这篇关于在ORMLite中更改架构时删除列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆