如何在 Android 中进行数据库模式迁移? [英] How to do database schema migrations in Android?

查看:24
本文介绍了如何在 Android 中进行数据库模式迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有在 Android 上进行数据库架构迁移的标准方法?例如,用户安装了我的 Android 应用程序的较新版本,但新版本需要对数据库架构进行更新(并且擦除用户的数据库并重新开始不是一种选择!).所以我需要在我的新版本第一次运行时运行一些 ALTER 语句和/或复制表.

Is there a standard way to do database schema migrations on Android? For example, user installs newer version of my Android app but the new version needs to make updates to the database schema (and wiping the user's database and starting over is not an option!). So I need to run some ALTER statements and/or copy tables the first time my new version runs.

推荐答案

是的,SQLiteOpenHelper 支持在不同版本的数据库模式之间迁移.

Yes SQLiteOpenHelper has support for migrating between different versions of DB schemas.

升级是通过实现来完成的

Upgrading is done by implementing

public abstract void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)

并且还支持回滚到以前的版本:

And Rolling back to a previous version is also supported :

public abstract void onDowngrade (SQLiteDatabase db, int oldVersion, int newVersion)

这篇关于如何在 Android 中进行数据库模式迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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