2016-04-26 58 views
0

我是IONIC的新手,想做一些數據庫操作。爲此,我選擇了sqlite作爲數據庫。所以下面的教程Tutorial Link我寫我app.jsIONIC中的Sqlite顯示錯誤TypeError:n.sqlitePlugin未定義

db = $cordovaSQLite.openDB("nextflow.db"); //line 29 

$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS ta_sheets(id integer primary key AUTOINCREMENT, name varchar(255))"); 
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS tas(id int primary key, name varchar(255), description TEXT, amount DECIMAL(10,2), date DATE)"); 

但不知何故,它沒有創造任何數據庫和控制檯顯示錯誤TypeError: n.sqlitePlugin is undefined

.openDB()   ng-cordova.min.js:9 
<anonymous>   app.js:29 
.$get</t.ready/<() ionic.bundle.min.js:384 
r()     ionic.bundle.min.js:22 
o()     ionic.bundle.min.js:22 

的哪些錯誤?我完全迷失了。任何幫助/提示高度讚賞。

+0

這是從設備或瀏覽器的錯誤? – Akis

回答

0
$ionicPlatform.ready(function (ionic, $ionicPlatform) { 

     if (window.cordova && window.cordova.plugins.Keyboard) { 
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
     } 
     if (window.StatusBar) { 
      StatusBar.styleDefault(); 
     } 

     if (window.cordova) { 
      db = $cordovaSQLite.openDB({ name: "nextflow.db" }); //device 
      console.log("correct"); 
     } else { 
      db = window.openDatabase("nextflow.db", '1', 2024 * 2024 * 100, function() { }); // browser 
      console.log("correct"); 
     } 

     $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS ta_sheets(id integer primary key AUTOINCREMENT, name varchar(255))"); 
     $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS tas(id int primary key, name varchar(255), description TEXT, amount DECIMAL(10,2), date DATE)");  


    }); 

並且還在您的App.js或控制器中注入['$ window','$ ionicPlatform','$ cordovaSQLite']。

0

這是因爲您可能試圖從瀏覽器執行您的代碼。當您在真實設備上安裝時,SQLite plugin將僅適用。

從教程複製粘貼您提供:

So everything should ready now. Build the project then run this on your iOS or Android device.

0

由於SQL精簡版插件和的WebSQL具有相同的API,通常我裹在調用數據庫中的一類/服務並檢查我是否在cordova應用程序或Web瀏覽器中。

if (isIOSOrAndroid()){ 
    return this.executeQueryDeviceInternal(query, parameters); 
} 
else { 
    return this.executeQueryWebSql(query, parameters); 
} 

executeQueryDeviceInternal(query, parameters){ 
    return this.$cordovaSQLite.execute(this.getDatabaseConnection(), query, parameters); 
} 

executeQueryWebSql(query, parameters){ 
    var self = this; 
    return new Promise(function(resolve, reject){ 
     self.getDatabaseConnection().transaction(tx => { 
      tx.executeSql(query, parameters, function(tx, results){ 
       resolve(results); 
      }, function(transaction, error){ 
       reject(error); 
      }); 
     }); 
    }); 
} 
相關問題