0
votes

so I'm trying to get a pre-populated database working in my ionic app.

I'm using Cordova-sqlite-storagefor sqlite and cordova-plugin-dbcopy to copy the db into the phone's internal storage, but it's not working.

What is (or seems to be) working:

  • window.plugins.sqlDB.copy("Test.db", 0, pass, fail) returns the pass function the first the and fails as expected the second time (due to it already existing)
  • db = window.sqlitePlugin.openDatabase({name: "Test.db"}); I know works, because the following query works: (next bullet)

  • $cordovaSQLite.execute(db, "SELECT * FROM sqlite_master WHERE type='table'") works, but only returns the table android_metadata not my test table called names (which I made sure, is in the Test.db file)

What is not working

  • As mentioned above, I only get the starter table from my query, not the one that should be there

  • As a result, $cordovaSQLite.execute(db, "INSERT into names VALUES(2, 'hello')")gives me Error: no such table: names (code1):, while...

Perhaps I don't have the Test.db file in the right location. I've put it in the www folder, the platforms > android > assets folder and in the www > js folder with no luck (it's in all of them now).

Here is my code:

"use strict";

angular.module('starter').controller('AboutController', function(
  $scope,
  $cordovaSQLite
) {


  $scope.$on('$ionicView.beforeEnter', function() {
    var db = null;

    if (window.sqlitePlugin) {

      // copy the database
      window.plugins.sqlDB.copy("Test.db", 0,

        function() {
          // copy success, if not yet been copied
          console.log("Copy Success");
          db = window.sqlitePlugin.openDatabase({
            name: "Test.db"
          });

          execute(db)
        },
        function(err) {
          // copy error, if it has already been copied

          db = window.sqlitePlugin.openDatabase({
            name: "Test.db"
          });
          console.log("Copy Fail: " + JSON.stringify(err));

          execute(db)
        })
    }
  });


  function execute(db) {
    $scope.dbData += "in execute;  ";


    $cordovaSQLite.execute(db, "INSERT into names VALUES(2, 'hello')")
      .then(function(res) {
          console.log("sql1: " + res.rows.length);
        },
        function(err) {
          console.log("sql1: " + err);
        });


    $cordovaSQLite.execute(db, "SELECT * FROM sqlite_master WHERE type='table'")
      .then(function(res) {

          for (var i = 0; i < res.rows.length; i++)
            console.log(res.rows.item(i).name);
        },
        function(err) {
          //$scope.showAlert("failed2?", err);
          console.log("sql2: " + err);

        });
  }




})

Any help would be greatly appreciated. Thank you.

1

1 Answers

0
votes

Found out the issue. While working on trying to get it running, I had downloaded many sqlite plugins (such as sqlite2 and sqlite-ext). One of these were conflicting somehow with the basic sqlite plugin. I simply removed all sqlite plugins and only installed

https://github.com/litehelpers/Cordova-sqlite-storage

and now it's working.