我正在尝试使用SQLite-Plugin将Phonegap 5.4与SQLite连接到Android上[https://github.com/litehelpers/Cordova-sqlite-storage]
首先,我创建我的项目并使用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/]网站上,我没有看到任何关于存储或数据库的部分 .
有什么解决方案吗?
1 回答
解决方案是:只需编写您的数据操作代码this,因为存储模块会自动注入您的项目中 .