如何从 cordova-sqlite 同步获取数据? 我正在解决这个问题。我在脑海中提出了很多问题。为什么 sqlite 不是序列化模式。意思是:为什么它不是逐行执行..?
我就是这样解决的。。
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 然后,表名。在控制台中我得到这个错误
未捕获的错误:无法在初始化之前调用 listview 上的方法;试图调用方法“刷新”