0

我在我的项目“ActiveAndroid”中使用。我有一个模型,我保留。这是一个模型:

@Table(name="Params")
public class Params extends Model {

    public Params() {
        super();
    }

    @Expose
    @Column(name="height")
    @SerializedName("heigth")
    public int heigth;

    @Expose
    @Column(name="weight")
    @SerializedName("weight")
    public int weight;
}

这里全部保存:

public void success(User user, Response response) {
               user.params.save();
               user.save();
                ActiveAndroid.clearCache();
            }

一切正常!但是如果我想在模型中添加另一个字段:

@Expose
    @Column(name="strong")
    @SerializedName("strong")
    public int strong;

我收到一个错误:

android.database.sqlite.SQLiteException: table Params has no column named strong (code 1): , while compiling: INSERT INTO Params(Id,weight,height,strong) VALUES (?,?,?,?)

我当然知道为什么会出现这个错误。因为表中没有这样的列。这就是问题,如何添加此列???

现在我已经尝试过:

  1. 完全删除程序,再次编译,运行,一切正常!因为它从该列创建了一个新表。

  2. 尝试更改清单数据库中的版本,但这并没有成功。

我知道在升级的方法中正常使用数据库可以更改数据库的版本和重组表。但是如何在 ORM "ActiveAndroid" 中做到这一点?

4

1 回答 1

4

有一种东西被称为Migration,在 Active Android 中你可以这样

  1. 在模型中添加字段(您已经这样做了)

  2. 更改 AndroidManifest.xml 元数据的数据库版本

  3. 编写您的迁移脚本。为您的脚本命名[newDatabaseVersion].sql,并将其放在目录中[YourApp’sName]/app/src/main/assets/migrations。在我的具体示例中,我将创建文件[MyAppName]/app/src/main/assets/migrations/2.sql.

例如ALTER TABLE Items ADD COLUMN Priority TEXT;

于 2015-01-27T11:31:37.303 回答