Javascript SQLite插件Cordova基本代码
我对Cordova&Sqlite不熟悉,但我写了一些代码,但我不知道它有什么问题?有什么建议吗? 我总是从Javascript调试器获得以下输出:Javascript SQLite插件Cordova基本代码,javascript,sqlite,cordova,visual-studio-2015,cordova-plugins,Javascript,Sqlite,Cordova,Visual Studio 2015,Cordova Plugins,我对Cordova&Sqlite不熟悉,但我写了一些代码,但我不知道它有什么问题?有什么建议吗? 我总是从Javascript调试器获得以下输出: //等待Cordova加载 文件。添加的监听器('deviceready',onDeviceReady,false); var output=document.getElementById('outputField'); //科尔多瓦准备好了 函数ondevicerady(){ window.sqlitePlugin.openDatabase({
//等待Cordova加载
文件。添加的监听器('deviceready',onDeviceReady,false);
var output=document.getElementById('outputField');
//科尔多瓦准备好了
函数ondevicerady(){
window.sqlitePlugin.openDatabase({name:'test.db',位置:2},函数(db){
output.innerHTML+='-已创建/打开数据库';
数据库事务(功能(tx){
tx.executeSql(tx,“创建表localStorage2(如果不存在)(键唯一,值)”);
});
output.innerHTML+='-创建了表localStorage2';
storeValue(db,‘localStorage2’、‘testKey’、‘testValue’);
output.innerHTML+='-插入伪值';
output.innerHTML+=''+readValue(db,'localStorage2','testKey');
});
}
函数storeValue(数据库、表、键、值){
数据库事务(功能(tx){
tx.executeSql(tx,'插入'+table+'(键,值)值(“'+key+'”,“'+value+'”));
});
}
函数readValue(数据库、表、键){
数据库事务(功能(tx){
返回db.executeSql(tx,'从'+table+'中选择*,其中key=“'+key+'”);
});
}
如果你正在测试一个新的插件、库等等,最好的方法是阅读文档,玩一点示例,然后根据需要逐步扩展它
SQLite插件是事件驱动的,也就是说,您必须等到工作完成
你这样做,而这个不起作用:
var the_result = mySQL_Job();
function mySQL_Job(){
db.readTransaction(function(tx) {
return db.executeSql(…);
});
}
正确的方法是:
mySQL_Job();
function mySQL_Job(some_values){
tx.executeSql("SELECT * FROM myTable", [], function(tx, res) {
//Here goes your result handling, like inserting values in your html
}, function(error) {
console.log('SQLite error: ' + error.message);
});
}
您必须为每个sql作业执行此操作,请参阅以下文档:
如果您有很多查询,那么最好使用promises:您是否使用过SQLite的插件?如果是,那么您也可以在不使用任何插件的情况下使用SQLite。检查链接:请阅读此处的文档:您的主要问题是,您的代码不是事件驱动的。您不能在事件驱动脚本中使用返回值。@Dhruv我使用的是Joerg@Joerg你所说的事件驱动是什么意思?你能举个小例子吗
mySQL_Job();
function mySQL_Job(some_values){
tx.executeSql("SELECT * FROM myTable", [], function(tx, res) {
//Here goes your result handling, like inserting values in your html
}, function(error) {
console.log('SQLite error: ' + error.message);
});
}