2016-11-23 62 views
0

我已經寫在我的控制器

$scope.$on('$ionicView.afterEnter', function(event, data){ 
     var db = null; 
     db = $cordovaSQLite.openDB({ name: 'app.db' }); 
     $cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text)"); 
}); 

下,我也在我的項目安裝sqlite的插件。

但每當我運行我的控制器,我正在剛開錯誤$cordovaSQLite is not defined

回答

1

要驗證,無論是Javascript和sqlite的插件的本地部分安裝在您的應用程序:

window.sqlitePlugin.echoTest(successCallback, errorCallback); 

監聽onDeviceReady事件,然後使用sqlite。在運行功能

document.addEventListener("deviceready", yourCallbackFunction, false); 
function yourCallbackFunction() 
{ 
    // Sqlite API implementation here 
} 
+0

ü可以顯示如何做到這一點? – lakshay

+0

@lakshay:請檢查我編輯的答案。 – Sagar

+0

你也可以告訴我我註冊我的sqlite插件除了.run()在我的app.js文件中的任何地方。 – lakshay

0

使用下面的命令來安裝插件源碼,

科爾多瓦插件添加 https://github.com/litehelpers/Cordova-sqlite-storage.git

然後在app.js頁,申報$ cordovaSQLite &添加你的db代碼,

.run(function($ionicPlatform,$cordovaSQLite,$timeout,$ionicLoading, $cordovaDialogs){ 
     var db = null; 
     db = $cordovaSQLite.openDB({ name: 'app.db',location:1});  
     $cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text)"); 

    }) 

希望這會有助於!

0

在.RUN功能,你應該初始化數據庫等爲:

  if (window.cordova) { 
       // App syntax 
       self.db = window.sqlitePlugin.openDatabase(CONFIG.DB_NAME); 
      } else if (window.openDatabase) { 
       self.db = window.openDatabase(CONFIG.DB_NAME, '1.0', 'database', -1); 
      }