brodycj / cordova-sqlite-ext

A Cordova/PhoneGap plugin to open and use sqlite databases on Android/iOS/macOS/Windows with REGEXP (Android/macOS/iOS) and pre-populated databases (Android/iOS/macOS/Windows)
Other
72 stars 55 forks source link

Database lost on app update #61

Open Turv opened 7 years ago

Turv commented 7 years ago

Hi,

I have an Android/IOS Ionic (cordova) app, the initial database when created is seeded from a list of SQL Queries. I then store various presets, settings, and stats in this database once created.

When an update is released to the app store and the app is updated, the SQL Database is completely lost so I lose any settings, and stats that had already been created, as the database is effectively overwritten.

I create the database using the following call, as I'm storing the database in a central location (Library) I'd expect the database to still exist on an app update and thus never be lost. Can you advise if I am creating the database correctly, and that I am correct in presuming I should be able to have a persistent database with app updates?

_db = window.sqlitePlugin.openDatabase({ name: "app.db", iosDatabaseLocation: 'Library', location: 'default', createFromLocation: 1 });

brodycj commented 7 years ago

You are using both location and iosDatabaseLocation which is wrong. As already documented you have to use one or the other.

Turv commented 7 years ago

Hi @brodybits

As this is a cross platform app, with the same codebase being used for both Android and iOS. Please can you tell me how I would handle having a persistent database on both platforms?

I wouldn't want to strictly only use iosDatabaseLocation if that's not going to work on Android, or are you suggesting that instead we need to have a different openDatabase call per platform? I.e.

if (ionic.Platform.isIOS())
    _db = window.sqlitePlugin.openDatabase({ name: "app.db", iosDatabaseLocation: 'Library', createFromLocation: 1 });
else 
    _db = window.sqlitePlugin.openDatabase({ name: "app.db", location: 'default', createFromLocation: 1 });

If the above is the case, can you advise if location: 1 (integer) is actually the correct option for Android too, to be in a shared location for a persistent database?

Thanks

brodycj commented 7 years ago

On Android and Windows the plugin always stores the database in the same place. The iosDatabaseLocation setting makes it clear that you change the database location on iOS only.

Turv commented 7 years ago

@brodybits

Thanks for both your quick responses. In which case I will change my openDatabase command to just use the iosDatabaseLocation _db = window.sqlitePlugin.openDatabase({ name: "app.db", iosDatabaseLocation: 'Library', createFromLocation: 1 });

If there is no way to change the location on Android, do you happen to know by default where this is stored, and if an app update may cause a similar overwrite of the database (I.e. I instead need to change the infrastructure of the app to sync the data with the web opposed to a local database).

brodycj commented 7 years ago

https://stackoverflow.com/questions/15326455/what-is-the-default-database-location-of-an-android-app-for-an-unrooted-device/15326498#15326498