安卓:难度在提升SQLite表 [英] Android: Difficulty in upgrading SQlite table

查看:156
本文介绍了安卓:难度在提升SQLite表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作表中运行的应用程序叫兽。在第一次创建此表是由简单的_id和animal_name列。

I have an application running with a working table called ANIMAL. Upon first creating this table it consisted simply of _id and animal_name columns.

现在我想扩大它,包括animal_biography列,但我有一个小difficulty.At开始我以为我升级我的CREATE_TABLE声明,包括动物生物只是一个案件:

Now I am trying to expand on it, including a animal_biography column, however I am having a little difficulty.At first I thought I was just a case of upgrading my CREATE_TABLE statement to include the animal bio:

private static final String DATABASE_CREATE = 



            "create table " + ANIMALS_TABLE +

            " (_id integer primary key autoincrement, " + 

            "animal_name text not null, " +

            "biography text not null);"; 

不过,在看的logcat它告诉我列的传记试图插入它时,不存在。

however, looking at the logcat it tells me the column biography does not exist when trying to insert into it.

现在,我已经尝试使用onUpgrade()和包括code升级数据库

Now, I have tried to upgrade the database by using the onUpgrade() and including the code

db.execSQL("ALTER TABLE" + DATABASE_NAME);
db.execSQL(DATABASE_CREATE);

但这不是解决问​​题either.Does人对如何去解决这个问题?谢谢你的任何指针。

but this is not solving the problem either.Does anyone have any pointers on how to go about fixing this problem?Thanks.

推荐答案

。你需要实现方法的onCreate onUpgrade 并提供类的构造数据库的最新版本。当更新表只是增加数据库的版本号,请在的onCreate 法新表的创建语句,并把 ALTER TABLE onUpgrade 方法来更新表中的previous版本。当机器人检测数据库版本不匹配,它会调用 onUpgrade 自动方法。见例如:

If you are using SQLiteOpenHelper it's easy to upgrade a table. You need to implement methods onCreate and onUpgrade and provide current version of your database in class constructor. When updating your table just increment database version number, specify new table creation query in onCreate method and put ALTER TABLE to onUpgrade method to update previous version of table. When Android detects database version mismatch, it will call onUpgrade method automatically. See the example:

public class OpenHelper extends SQLiteOpenHelper {

    private final static int    DB_VERSION = 2;

    public TracksDB(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        final String CREATE_TBL =
            "create table " + ANIMALS_TABLE +
            " (_id integer primary key autoincrement, " + 
            "animal_name text not null, " +
            "biography text not null);";
             db.execSQL(CREATE_TBL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion < 2) {
            final String ALTER_TBL = 
                "ALTER TABLE " + ANIMALS_TABLE +
                " ADD COLUMN biography text not null;";
            db.execSQL(ALTER_TBL);
        }
    }
}

升级此方法是修改的表,而不会失去用户数据的正确方式,特别是如果该应用已被释放到公共已经

This method of upgrading is the correct way to modify tables without losing user data, especially if the app had been released to public already.

这篇关于安卓:难度在提升SQLite表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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