如何在ActiveAndroid中删除并重新创建数据库版本更新上的所有表? [英] How to drop and recreate all tables on DB version update in ActiveAndroid?

查看:268
本文介绍了如何在ActiveAndroid中删除并重新创建数据库版本更新上的所有表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中将ActiveAndroid用作Database系统.在当前阶段,我不想编写用于在更新数据库版本时将当前表迁移到已更改表的迁移文件.

I'm using ActiveAndroid in my application as the Database system. At current stage I don't want to write the migration files for migrating current tables to altered ones for when I update DB version.

相反,我要删除所有表并重新创建一组新表.

Instead what I want to do is to drop all tables and recreate a new set of tables.

不幸的是,通过挖掘ActiveAndroid进行的Database版本测试发生在初始化阶段,在该阶段中,我通过了ActiveAndroid Configuration对象来配置Database.

Unfortunately from digging in the ActiveAndroid the Database version test happens at initialization stage in which I pass a ActiveAndroid Configuration object to configure the Database.

此时,它们的实现仅检查资产文件夹中的迁移文件.我不介意创建迁移文件来执行所需的操作,但是问题是我不知道如何在数据库中初始化当前表以重新创建它们.

At this point their implementation only checks for migration files in the assets folder. I don't mind to create migrations file to do what I need but the problem is that I don't know how to get the current tables initialized in the DB to recreate them.

有人知道如何使用ActiveAndroid做到这一点吗?

Does some one knows how to do that with ActiveAndroid?

推荐答案

不确定我是否理解正确,但是如果您正在开发中,那么最简单的方法就是卸载应用程序,并让Active Android在安装时重新创建数据库架构再次.

Not sure if I understood correctly but if you are in development then the easiest is to just uninstall the app and let Active Android recreate the DB schema when you install it again.

在生产中,您将需要根据迁移脚本的文档使用迁移脚本.

When in production you will need to use the migration scripts as per their documentation.

我遇到了类似的问题,我需要强制重新创建一些表.不幸的是,您将需要手动进行操作.但是,您可以使用ActiveAndroid帮助器类来帮助您.

I faced a similar issue and I needed to force a few tables to be recreated. Unfortunately you will need to do it manually. However you can use the ActiveAndroid helper classes to help you out.

  1. 首先,您需要以某种方式处理旧表数据.
  2. 删除表格:

  1. First you need to process the old table data somehow.
  2. Drop the tables:

SQLiteUtils.execSql(如果存在表名,则删除表");

使用ActiveAndroid类重新创建表.

Recreate the table using the ActiveAndroid classes.

SQLiteUtils.execSql(SQLiteUtils.createTableDefinition(Cache.getTableInfo(YOUR_MODEL_CLASS.class))));

将旧数据插入到新创建的表中.

Insert the old data into your newwly create table.

这篇关于如何在ActiveAndroid中删除并重新创建数据库版本更新上的所有表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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