SQlite升级


public class MyHelper extends SQLiteOpenHelper {
    public static final String CREATE_BOOK="create table book("
            +"id integer primary key autoincrement,"
            +"author text,"
            +"price real,"
            +"pages integer,"
            +"name text)";
    private Context mContext;
    public MyDataBaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version)
    {
        super(context,name,factory,version);
        mcontext=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_BOOK);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        switch (oldVersion) {
            case 1:
                db.execSQL(CREATE_CATEGORY);
            case 2:
                db.execSQL("alter table Book add column category_id integer");
                default:

        }

    }
}

升级数据库方式一:

onUpgrade()方法是用于对数据库进行升级的。
假如现在要在数据库中添加一个STUDENT的表,如何在上述程序上修改呢?
直接在onCreate()方法中添加创建语句是不行的,因为数据库已经存在,OnCreate()方法不会被运行了。那最基础的办法是:
传入一个高一点的版本号,
在onUpgrade运行删除数据库的操作,在点击按钮就可以运行OnCreate()方法了。
传入一个高一点的版本号,就可以让onUpgrade()方法执行

public class MyDataBaseHelper extends SQLiteOpenHelper {
    public static final String CREATE_BOOK="create table book("
            +"id integer primary key autoincrement,"
            +"author text,"
            +"price real,"
            +"pages integer,"
            +"name text,"
            +"category_id integer)";

    public static final String CREATE_STUDENT="create table Student("
            +"id integer primary key autoincrement,"
            +"name text,"
            +"age integer)";
private Context mContext;
    public MyDataBaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version)
    {
        super(context,name,factory,version);
        mContext=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_BOOK);
        db.execSQL(CREATE_STUDENT);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      db.execSQL("drop table if exists Book");
      db.execSQL("drop table if exists Student");
      onCreate(db);
        }
    }
}

优点:写法简单,缺点会删除历史数据,造成隐患

升级数据库方式二:

增加数据库版本号(为每一个版本号赋予它各自的更改的内容),在onUpgrade()方法中对当前数据库的版本号进行判断,再执行相应的改变就可以了。

public class MyDataBaseHelper extends SQLiteOpenHelper {
    public static final String CREATE_BOOK="create table book("
            +"id integer primary key autoincrement,"
            +"author text,"
            +"price real,"
            +"pages integer,"
            +"name text,"
            +"category_id integer)";

    public static final String CREATE_STUDENT="create table Student("
            +"id integer primary key autoincrement,"
            +"name text,"
            +"age integer)";



    public MyDataBaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version)
    {
        super(context,name,factory,version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_BOOK);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        switch (oldVersion) {
            case 1:
                db.execSQL(CREATE_STUDENT);
            case 2:
                db.execSQL("alter table Book add column category_id integer");
                default:

        }

    }
}

SQLite数据库只允许增加表字段(alter table my_table add field_name field_type;),不允许修改和删除表字段。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容