简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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