如何在添加新列后用activeandroid更新表 [英] How to update table with activeandroid after adding a new column

查看:498
本文介绍了如何在添加新列后用activeandroid更新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您阅读和帮助:)

我使用的是ActiveAndroid 3.0 - Android 2.2 - 在我的应用程序中,我有一个名为用户的模型。最初我只创建了(id,名称,密码)列/属性的模型,我在模拟器上运行应用程序,它的工作。

I am using ActiveAndroid 3.0 - Android 2.2 - In my app I have a model called "user". initially I only created the model with (id, name, passcode) columns/attributes, I ran the app on the emulator, and it worked.

@Table(name = "user")
public class User extends Model {
  @Column (name = "name")  
  public String name;
  @Column (name = "pass_code")
  public String passCode;
}

然后我添加了一个新的列/属性 - profileImage。我在模拟器上运行应用程序,我得到SQLite列profileImage不存在错误。

Then I added a new column/attribute - profileImage. I ran the app on the emulator and i got SQLite column profileImage does not exist error.

@Table(name = "user")
public class User extends Model {
  @Column (name = "name")  
  public String name;
  @Column (name = "pass_code")
  public String passCode;
  @Column (name = "profile_image")
  public String profileImage;
}



我尝试更改ActiveAndroid数据库名称和更新数据库版本属性清单文件。

I have tried changing the ActiveAndroid db name and updating the db version properties in the manifest file.

<meta-data android:name="AA_DB_NAME" android:value="my_app.db" />
<meta-data android:name="AA_DB_VERSION" android:value="2" />

但我不断收到列不存在错误。

But I keep getting the column does not exist error.

ERROR   AndroidRuntime  Caused by: android.database.sqlite.SQLiteException: 
no such column: profile_image: , while compiling: SELECT * FROM user WHERE profile_image = ?

我也试过,从模拟器卸载应用程序,重新启动模拟器,调用ActiveAndroid.cacheClear ()。到目前为止没有一个工作。

I have also tried, un-installing the app from emulator, restarting the emulator, calling ActiveAndroid.cacheClear(). So far none of this worked.

我真的很感激任何帮助。感谢。

I really appreciate any help. thanks.

推荐答案

从它的声音,你缺少升级脚本来迁移你的 表到新模式。升级脚本基本上包含有关如何从数据库的旧状态到新的状态的说明。在你的具体情况下,你想告诉SQLite它应该在现有用户表中添加一个新的列 profile_image

From the sounds of it, you're missing the upgrade script to migrate your user table to the new schema. The upgrade scripts basically contains the instructions on how to get from the old state of the database to the new one. In your specific case, you'll want to tell SQLite that it should add a new column, profile_image, to the existing user table.

要将数据库从模式1升级到2,将 AA_DB_VERSION 值添加到 2 给升级脚本命名 2.sql 。将文件保存在 assets / migrations 下,并提供以下内容:

To upgrade your database from schema 1 to 2, bump up the AA_DB_VERSION value to 2 and give the upgrade script the name 2.sql. Save the file under assets/migrations and give it the following content:

ALTER TABLE user ADD profile_image TEXT;

脚本可以包含任何一组可以由 SQLiteDatabase 通过 execSQL(...)

The scripts may contains any set of SQL statements that can be executed by the SQLiteDatabase through execSQL(...).

有关使用Active Android的模式迁移的更多详细信息,请参见 GitHub项目页面上的wiki

More details on schema migrations with Active Android can be found in the wiki on the GitHub project page.

这篇关于如何在添加新列后用activeandroid更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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