2016-12-16 38 views
0

在用戶突然從我的應用程序切換到另一個應用程序或重新啓動手機後,我正面臨着從先前登錄中讀取已設置數據的問題。我成功登錄後設置的數據會保存在SQLite數據庫中。在Android登錄時從SQLite離子讀取數據

.controller('LoginCtrl', function($scope, $ionicPopup, $state,$http,ServerEndPoint,localStorageService,$cordovaGeolocation,$ionicActionSheet,dataShare,$ionicPush,loading,$rootScope,$cordovaSQLite) { 
 
     
 
     $scope.data = {}; 
 
     
 
      //Does not work 
 
      $scope.init = function() 
 
      { 
 
       $scope.load(); 
 
      }; 
 

 
      if(localStorageService.get("tradie_id") !== null && localStorageService.get("phone_no") !== null) { 
 
      $state.go('menu.map'); 
 
      } 
 
      
 
      //This is called from login form submit button click 
 
      $scope.authenticateUser = function(loginForm){ 
 
       //Authenticating user from the server, after successful login 
 
       //This one works 
 
       $scope.addInfo(res.data.user_id,res.data.first_name,res.data.phone_no,status); 
 
       
 
       $state.go('menu.map'); 
 
      } 
 
    
 
    $scope.addInfo = function(user_id,first_name,phone_no,status){ 
 
    var query = "INSERT INTO user_data(user_id,first_name,phone_no,status) VALUES(?,?,?,?)"; 
 
    $cordovaSQLite.execute(db,query,[user_id,first_name,phone_no,status]); 
 
    $scope.load(); 
 
    } 
 
    
 
    $scope.load = function(){ 
 
    
 
    $scope.alldata = []; 
 
    $cordovaSQLite.execute(db,"SELECT * FROM user_data").then(function(result){ 
 
     if(result.rows.length) 
 
     { 
 
      for(var i=0;i<result.rows.length;i++) 
 
      { 
 
      $scope.alldata.push(result.rows.item(i)); 
 
      } 
 
       
 
       localStorageService.set("user_id", $scope.alldata[0].tradie_id); 
 
       localStorageService.set("first_name", $scope.alldata[0].first_name); 
 
       localStorageService.set("phone_no", $scope.alldata[0].phone_no); 
 

 
     }else 
 
     { 
 
      console.log("No data found"); 
 
     } 
 
    },function(error){ 
 
     console.log("error "+err); 
 
    }) 
 
    } 
 
      
 
})

任何建議或指針樣品源代碼是高度讚賞。我使用的離子版本1.

回答

0

我想你沒有創建或應用程序時,先準備好打開DB:

var db = $cordovaSQLite.openDB({ name: "my.db" });