How can I fetch data synchronously from cordova-sqlite? 我正在解决这个问题。我在脑海中提出了很多问题。为什么sqlite不是序列化模式.Mean:为什么它不是逐行执行..?
我这样解决..
var db = "";
//will create database Dummy_DB or open it
//function will be called when device ready
var caseNames=new Array();
function onDeviceReady() {
db = window.openDatabase("Casepad", "1.0", "Casepad", 200000);
if (window.localStorage.getItem("isAddSomeData") == "yes") {
db.transaction(getallTableData, errorCB);
/*db.transaction(function (tx) {
tx.executeSql('SELECT * FROM a', [],
// tx.executeSql('SELECT EXISTS(SELECT * FROM b)', [],
function(tx, results) {
t=results.rows.length;
alert(t)
});
});*/
}
}
function insertData() {
db.transaction(createTable, errorCB, afterSuccessTableCreation);
}
//create table and insert some record
function createTable(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS CaseTable (id INTEGER PRIMARY KEY AUTOINCREMENT, CaseName VARCHAR(100) unique NOT NULL ,CaseDate INTEGER ,TextArea VARCHAR(200) NOT NULL)');
tx.executeSql('INSERT OR IGNORE INTO CaseTable(CaseName,CaseDate,TextArea) VALUES ("' + $('.caseName_h').val() + '", "' + $('.caseDate_h').val() + '","' + $('.caseTextArea_h').val() + '")');
}
//function will be called when an error occurred
function errorCB(err) {
navigator.notification.alert("Error processing SQL: " + err.code);
}
//function will be called when process succeed
function afterSuccessTableCreation() {
console.log("success!");
db.transaction(getallTableData, errorCB);
}
//select all from SoccerPlayer
function getallTableData(tx) {
tx.executeSql('SELECT * FROM CaseTable', [], querySuccess, errorCB);
}
/*function querySuccess(){
for (var i = 0; i < len; i++) {
alert(result.rows.item(0).CaseName)
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM "'+result.rows.item(0).CaseName+'"', [],
// tx.executeSql('SELECT EXISTS(SELECT * FROM b)', [],
function(tx, results) {
t=results.rows.length;
alert(t)
});
});
}
}*/
function querySuccess(tx, result) {
var len = result.rows.length;
$('#folderData').empty();
for (var i = 0; i < len; i++) {
test=result.rows.item(i).CaseName;
if(test=="a"){
var t=Test(test);
alert("yes" +t)
}
$('#folderData').append(
'<li class="caseRowClick" id="' + result.rows.item(i).id + '" data-rel="popup" data-position-to="window">' + '<a href="#">' + '<img src="img/Blue-Folder.png">' + '<h2>' + result.rows.item(i).CaseName + '</h2>' + '<p>' + result.rows.item(i).TextArea + '</p>' + '<p>' + result.rows.item(i).CaseDate + '</p>' + '</a>' +
'<span class="ctrl togg"><fieldset data-role="controlgroup" data-type="horizontal" data-mini="true" ><button class="edit button_design">Edit</button><button class="del button_design">Delete</button></fieldset><span>'+'</li>'
);
}
$('#folderData').listview('refresh');
}
function Test(test){
alert(test)
db.transaction(function (tx) {
$yoursql = 'SELECT * FROM "'+test+'"';
tx.executeSql($yoursql, [], function (tx, results) {
return results.rows.length+"TableName"+test;
});
});
}
运行后我将首先 yesundefined 然后,表名.in console我收到此错误
未捕获错误:无法在初始化之前调用listview上的方法;试图调用方法'刷新'
答案 0 :(得分:1)
改变这个:
$('#folderData').listview('refresh');
到此:
$('#folderData').listview().listview('refresh');
在某些情况下,必须先使用 listview()
初始化列表视图,然后才能使用 listview('refresh')
进行刷新。
答案 1 :(得分:0)
$('#ID').trigger('create');
然后
$('#ID').append(APPEND_CONTENT).listview('refresh');