2016-09-19 36 views
1

我是新來的一般使用科爾多瓦和移動開發,但我的代碼中有一個非常奇怪的行爲。我使用ngCordova的SQLite插件(我使用Ionic),我想要做的事情非常簡單:如果存在一個表,那麼放棄它或創建它,如果它不存在。我已經爲數據庫操作創建了一個服務(我不知道是否是最好的方法來做到這一點,但它保持了與控制器分離的那種邏輯)。科爾多瓦的SQLite插件:代碼向後運行

的邏輯是這樣的:

app.js

angular.module('starter', ['ionic', 'ngCordova', 'starter.controllers', 'starter.services']) 
.run(function($ionicPlatform, $ionicLoading, $timeout, initialService) { 
    $ionicPlatform.ready(function() { 
     if (!initialService.hasUsers()) { // If there's no table called Usuarios 
      initialService.createDefaultUser(); // Create table and a record 
     } else { 
      $ionicLoading.show({ 
       template: 'Restableciendo...' 
      }); 
      initialService.resetDatabase(); // Droping table 

      $timeout(function() { 
       $ionicLoading.hide(); 
      }, 3000); 
     } 
    }); 
}) 

services.js

angular.module('starter.services', ['ionic', 'ngCordova']).service('initialService', function($cordovaSQLite, $ionicPopup) { 
    return { 

     // Check if Usuarios table exists 
     hasUsers: function() { 
      if (ionic.Platform.isAndroid()) { 
       db = $cordovaSQLite.openDB({ 
        name: "com.pos.db", 
        iosDatabaseLocation: 'default' 
       }); 
      } else { 
       db = $cordovaSQLite.openDB({ 
        name: "com.pos.db", 
        location: 2, 
        createFromLocation: 1 
       }); 
      } 

      var returnValue; 
      db.transaction(
       function(tx) { 
        tx.executeSql("SELECT name FROM sqlite_master WHERE type='table' AND name='Usuarios'", [], function(tx, result) { 
         console.log('Existen ' + result.rows.length + ' tablas con el nombre Usuarios'); 
         returnValue = (result.rows.length) ? true : false; 
        }); 
       } 
      ); 
      return returnValue; 

     }, 

     // Creates the Usuarios table and a testing record 
     createDefaultUser: function() { 
      var returnValue; 
      console.log("creando tabla de usuarios"); 
      if (ionic.Platform.isAndroid()) { 
       db = $cordovaSQLite.openDB({ 
        name: "com.pos.db", 
        iosDatabaseLocation: 'default' 
       }); 
      } else { 
       db = $cordovaSQLite.openDB({ 
        name: "com.pos.db", 
        location: 2, 
        createFromLocation: 1 
       }); 
      } 

      db.sqlBatch([ 
       'DROP TABLE IF EXISTS Usuarios', 
       'CREATE TABLE Usuarios (idUsuario INTEGER PRIMARY KEY AUTOINCREMENT, usuario TEXT NOT NULL, tipoUsuario NUMERIC NOT NULL DEFAULT 0, password TEXT)', 
       "INSERT INTO Usuarios (idUsuario,usuario,tipoUsuario,password) VALUES (1,'mike',0,'123');", 
      ], function() { 
       returnValue = true; 
      }, function(error) { 
       returnValue = false; 
      }); 
      return returnValue; 
     }, 

     // Drops the table 
     resetDatabase: function() { 
      var returnValue = false; 
      console.log("Eliminando tabla de usuarios"); 
      db.transaction(
       function(tx) { 
        tx.executeSql("DROP TABLE IF EXISTS Usuarios", [], function(tx, result) { 
         returnValue = true; 
        }); 
       } 
      ); 
      return returnValue; 
     } 
    }; 
}); 

我與我的手機和Chrome控制檯和調試代碼的順序與執行順序不同:

Error

如何確保所有這些操作都按正確順序進行?

+0

看看我爲這個問題提供的答案:http://stackoverflow.com/questions/39526355/unable-to-create-sqlite-tables-using-javascript-in-for-loop/39528291#39528291 – nyluje

+0

I認爲你的問題是相似的,你需要使用'$ .deferred()'來捕獲事件完成時的情況。 – nyluje

+0

我在[ngCordova包裝](https://github.com/driftyco/ng-cordova/blob/master/src/plugins/sqlite.js)中看到過,它們使用'$ .defer',但我在cordova的插件代碼中找不到類似的東西。他們必須做到這一點... – MikeVelazco

回答

0

我想你的項目嵌入jquery。

如果您想鏈接查詢的執行,您必須使用$ .deferred()(和promise())(請先閱讀它以瞭解基本內容)。

然後我建議你使用JS對象方法。

JS Object approach and $.deferred apply to WebSQL (quite similar to SQLite) are shown as an answer to the question in this link

+0

僅作爲對任何正在尋找僅支持Angular的解決方案的人的評論,請檢查[$ q Docs](https://docs.angularjs.org/api/ng/service/$q) – MikeVelazco

相關問題