如何使用 Phonegap 5.4 和 SQLite-Plugin

How to working with Phonegap 5.4 and SQLite-Plugin

我正在尝试使用 SQLite-Plugin [https://github.com/litehelpers/Cordova-sqlite-storage]

在 Android 上将 Phonegap 5.4 与 SQLite 连接起来

首先,我创建我的项目并使用 Phonegap CLI 导航到它

$phonegap create sqlite-demo
$cd sqlite-demo

之后,我运行安装SQLite插件的命令:

$phonegap plugin add https://github.com/litehelpers/Cordova-sqlite-storage --save

最后,我使用下面的代码来测试 [index.js 文件],但没有任何反应:

var app = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);        
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
    app.receivedEvent('deviceready');
    var db = window.sqlitePlugin.openDatabase({name: "my.db"}, 
    function() {
        alert('Connected');
    },
    function(err) {
        alert('Error');
    });
},
// Update DOM on a Received Event
receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
}
};

在 PhoneGap API [http://docs.phonegap.com/plugin-apis/] 网站上,我没有看到任何关于存储或数据库的部分。

有什么解决办法吗?

解决方案是:像这样编写你的数据操作代码this,因为存储模块是自动注入到你的项目中的。