Sqlite DB Android Backup/Restore

后端 未结 3 2035
悲&欢浪女
悲&欢浪女 2020-12-17 05:55

I wanted to know what the best practices are for backing up and restoring a SQLite DB on android. Currently I approach the issue by taking the DB to be backed up and using F

3条回答
  •  攒了一身酷
    2020-12-17 06:10

    I'd add one additional comment to Kingamajick's answer (the forum won't let me add it as an actually comment there). In the approach that simply copies the file, if a user ever restores the DB and there happens to be any data already in it, it will be overwritten. For example, if a user upgrades to a new phone, uses it for a while, and then restores the DB from their old phone, any data already on the new phone will be lost. That is one advantage to the complexity of reading the DB and writing it out to a file (XML or CSV, etc.).

    I posted another question (Android sqlite backup/restore without overwriting) in hopes someone had a better solution that avoided this problem, but so far it seems like there isn't one. Between that and the concerns ssuperz28 noted it seems like a much safer way to backup your DB is to write it out to xml and then read it and add it back in on restore.

    Also, https://stackoverflow.com/a/34477622/3108762 is the best of the other suggestions I've seen so far and promises a better way to approach this starting in Marshmallow.

提交回复
热议问题