简体   繁体   English

Java / Android SQL Helper类为什么在升级时删除表?

[英]Java/Android SQL Helper Class Why drop the table when upgrading?

For the majority of the examples and tutorials I have seen it is always suggesting to execute SQL to Delete the table if it exists in the onUpgrade method. 对于大多数示例和教程,我已经看到,总是建议执行SQL删除表(如果onUpgrade方法中存在表)。

Why would you want to delete the table as this would remove all the data, would it not be better to just replace the old DB version with the new version? 您为什么要删除表,因为这会删除所有数据,所以只用新版本替换旧数据库版本会更好吗?

This would something that I could not understand and no where online outlined the reason. 这是我无法理解的,在线上没有概述原因。

Thanks 谢谢

| | Sam | 山姆|

For simplicity. 为简单起见。 Dropping the old version and recreating a new version is simple and straightforward, though also destructive. 删除旧版本并重新创建新版本既简单又直接,尽管也具有破坏性。 In many cases, data loss like this is not a concern at development time. 在许多情况下,在开发时不必担心像这样的数据丢失。

Writing proper data migration code would be a topic for another example/tutorial as it inherently involves at least two versions of the database schema and thus the database helper. 编写适当的数据迁移代码将是另一个示例/教程的主题,因为它固有地涉及至少两个版本的数据库架构,因此也涉及数据库助手。 Including migration example in a simple do-this-to-get-started tutorial would just add unnecessary complexity. 在简单的“入门指南”教程中包含迁移示例只会增加不必要的复杂性。

Because they are tutorials: they assume you have no valuable data in them (or no data at all). 因为它们是教程,所以它们假定您中没有有价值的数据(或根本没有数据)。 In that case, the easiest way to upgrade a schema is to remove old tables and creating the new ones. 在这种情况下,升级架构的最简单方法是删除旧表并创建新表。

Don't take in mind it, you are right. 不要介意,你是对的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM