Search code examples
androidsqlitecordovacordova-plugins

Delete pre-populated sqlite database when update in cordova


I have completed my project which contains a pre populated sqlite database.

I have found this plugin and it works for me nicely. Lifehelper's products are truly remarkable and they have maintain pretty nice documentation.

However, when i open database, i use this format which is suggested their documentation for pre populated database.

var db = window.sqlitePlugin.openDatabase({name: "my.db", location: 'default', createFromLocation: 1});

However, my database is only for information. Application only read data from pre populated database. There is no any insert, update or delete query to keep per any user information.

In every new version pre populated data could modified, updated by me. So I need completely new app install along with new database when user will update their app.

But unfortunately, my app could not delete previous database. Changing version code also not working.

Someone suggest me, its about location which i am using to open database. But i can not understand how should dealing with it.

Help me, best answer is more important to me. Thanks.


Solution

  • To avoid increasing app size, you can do something like this:

    function cleanupDatabases() {
      var oldDatabases = ["2.1.0.db", "2.0.0.db"];
    
      if (window.sqlitePlugin !== undefined) {
        angular.forEach(oldDatabases, function(db) {
          window.sqlitePlugin.deleteDatabase({
            name: db,
            location: 2
          }, function() {
            console.log("%c " + db + " is deleted", "background: green; color: white");
          }, function(error) {
            console.log("%c " + db + " could not be deleted", "background: red; color: white", error);
          });
        });
      }
    };