Ionic + sqlite:无法访问ios上的sqlite表(cordova SQLite)

时间:2016-04-18 22:00:35

标签: ios sqlite cordova ionic-framework

我正在使用Ionic创建我的混合应用程序。

我使用sqlite作为数据库。在Android上,我可以毫无问题地访问我存储的所有数据,但在iOS上却失败了。我在xcode调试控制台中检索到以下错误(我正在使用iPhone 6s Plus进行测试):

[debug] OPEN database: rcsappmobile.db
[debug] Initializing SQLitePlugin
[debug] Detected docs path: /var/mobile/Containers/Data/Application/<UUID>/Documents
[debug] Detected Library path: /var/mobile/Containers/Data/Application/<UUID>/Library
[debug] no cloud sync at path: /var/mobile/Containers/Data/Application/<UUID>/Library/LocalDatabase
[debug] open full db path: /var/mobile/Containers/Data/Application/<UUID>/Library/LocalDatabase/rcsappmobile.db
[debug] Good news: SQLite is thread safe!
[debug] DB opened: rcsappmobile.db

[debug] SELECT av.id, av.name FROM app_venues av WHERE av.active ORDER BY av.venuename ASC
[debug] result: 
[debug] {"$$state":{"status":0}}
[debug] MultipleResultHandler
[debug] Default Error Handler -> Error processing SQL:  undefined: undefined
[debug] Default Error Handler -> Error processing SQL:  5: a statement error callback did not return false: no such table: app_venues

到目前为止我的问题。

编辑: 似乎我用来将数据库复制到其目标的插件删除了所有预先填充的内容!我通过xcode检查了数据库,它是空的!

这是我的代码:

controllers.js:

var dashboardCtrl = function ($scope, $ionicPlatform, $cordovaDevice, DbAccess) {
  console.log('dashboardCtrl');
  $ionicPlatform.ready(function () {
    console.log('dashboard -> platform ready');
      DbAccess.openDb();
  });
};
dashboardCtrl.$inject = ['$scope', '$ionicPlatform', '$cordovaDevice', 'DbAccess'];


var venuesCtrl = function ($scope, $ionicPlatform, $cordovaDevice, VenuesQueries) {
  console.log('venuesCtrl');
  $ionicPlatform.ready(function () {
    console.log('venue -> platform ready');
      VenuesQueries.getVenues().then(function(result) {
        $scope.outputs = result;
        var len = Object.keys(result).length;
        if(len > 0) {
          $scope.outputs = result;
        } else {
          console.log('No records found in database.');
        }
      }, function(error) {
        console.error(error);
      });
  });
};
venuesCtrl.$inject = ['$scope', '$ionicPlatform', '$cordovaDevice', 'VenuesQueries'];

service.js

var DbAccess = function ($q, $cordovaSQLite, $ionicPlatform, $cordovaDevice, $log) {
  var DbAccess = {};
  var db = {};
  var dbName = 'rcsappmobile.db';
  $ionicPlatform.ready(function () {
    console.log('Service: DbAccess -> platform ready');

      var platform = $cordovaDevice.getPlatform();
      // default: Library/LocalDatabase subdirectory - NOT visible to iTunes and NOT backed up by iCloud
      // Library: Library subdirectory - backed up by iCloud, NOT visible to iTunes
      // Documents: Documents subdirectory - visible to iTunes and backed up by iCloud
      var dbLocation = 'default';
      if (platform === 'Android') {
        dbLocation = 'default';
      }
      console.log('dbLocation: ', dbLocation);

      if (window.sqlitePlugin) {

        DbAccess.openDb = function() {
          console.log('Open database '+dbName);
          // open database
          function dbopen() {
            db = window.sqlitePlugin.openDatabase({
              name: dbName,
              iosDatabaseLocation: dbLocation
            });
          }
          dbopen();
        };

        DbAccess.DefaultErrorHandler = function (err) {
          $log.log("Default Error Handler -> Error processing SQL: ", err.code + ": " + err.message);
        };

        DbAccess.promisedQuery = function (query, successCB, errorCB) {
          var deferred = $q.defer();
          db.transaction(function (tx) {
            tx.executeSql(query, [], successCB(deferred), errorCB);
          }, errorCB);
          return deferred.promise;
        };

        DbAccess.SingleResultHandler = function (deferred) {
          return function (tx, results) {
            var len = results.rows.length;
            var output_results = [];
            for (var i = 0; i < len; i++) {
              output_results.push(results.rows.item(i));
            }
            deferred.resolve(output_results[0]);
          };
        };

        DbAccess.MultipleResultHandler = function (deferred) {
          console.log('MultipleResultHandler');
          return function (tx, results) {
            var len = results.rows.length;
            var output_results = [];
            for (var i = 0; i < len; i++) {
              output_results.push(results.rows.item(i));
            }
            deferred.resolve(output_results);
          };
        };

      }    
  });
  return DbAccess;
};
DbAccess.$inject = ['$q', '$cordovaSQLite', '$ionicPlatform', '$cordovaDevice', '$log'];

var VenuesQueries = function (DbAccess) {
  var VenuesQueries = {
    getVenues: getVenues
  };

  function getVenues(orderBy) {
    if(!orderBy) {
      orderBy = 'expires';
    }
    var query = "SELECT av.id, av.name FROM app_venues av WHERE av.active ORDER BY av.venuename ASC";
    return DbAccess.promisedQuery(query, DbAccess.MultipleResultHandler, DbAccess.DefaultErrorHandler);
  }

  return VenuesQueries;
};
VenuesQueries.$inject = ['DbAccess'];

1 个答案:

答案 0 :(得分:0)

我解决了。

问题是:我使用了两条不同的路径。 整个解决方案发布在Github上的插件页面上:cordova-plugin-dbcopy