我有一个表caseTable
,其中包含字段(ID,caseName,date)。该表中的每一行对应于以caseName字段命名的另一个表。我需要遍历caseTable
表并获取所引用表中行数的计数。
function onDeviceReady() {
db = window.openDatabase("Casepad", "1.0", "Casepad", 200000);
db.transaction(getallTableData, errorCB);
}
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 TEXT unique NOT NULL ,CaseDate INTEGER ,TextArea TEXT 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(tx, result) {
var len = result.rows.length;
var t;
$('#folderData').empty();
for (var i = 0; i < len; i++) {
/* *************************************************************
* Here i need to call a synchronous method which returns the
* number of rows in the result.rows.item(i).CaseName table
* ************************************************************* */
$('#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 + t+'</h2>' + '<p>' + result.rows.item(i).TextArea + '</p>' + '<p>' + result.rows.item(i).CaseDate + '</p>' + '<span class="ui-li-count">' + i + '</span></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');
}
在列表视图中显示“i”的值,我需要显示该表中有多少元素。我需要调用synchronize,因为我需要调用一些查询来计算此元素中“result.rows.item(i).CaseName”中元素的数量..?
举例......
DB Name Case Pad 表名称CaseTable
Let assume having entries in caseTable.
ID CaseName Case Date caseNote
1 Test 3/77/13 jgjgj
2 Test2 4/34/3 hsadkkadsh
Now I have two more table in DB
Test , Test2..
**Test** having entries like this
ID DocumentName Date Notes
1) ppp 7/33 asdhdfkdshf
2) asdjhad 9/44 dfjasgfsjfj
**Test2** having entries like this
ID DocumentName Date Notes
1) sad 7/4 asdhdfkdshf
2) assd 3/44 hhhhhh
3) asd 2/22 adgjad
现在测试,Test2的条目是2和3。
现在我需要获取CaseTable的数据。虽然fetch我需要计算我的casename表中的元素数量(Test,Test1)。这里只有
function getallTableData(tx) {
tx.executeSql('SELECT * FROM CaseTable', [], querySuccess, errorCB);
}
function querySuccess(tx, result) {
var len = result.rows.length;
var t;
$('#folderData').empty();
for (var i = 0; i < len; i++) {
Here i need to call synchronize method which call the number of element in in that result.rows.item(i).CaseName and insert it in this table
****************************************************************************
$('#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 + t+'</h2>' + '<p>' + result.rows.item(i).TextArea + '</p>' + '<p>' + result.rows.item(i).CaseDate + '</p>' + '<span class="ui-li-count">' + i + '</span></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');
}
我需要打印casename casedate case注意,元素数量 所以我的列表看起来像这样
Test 3/77/13 jgjgj 2
Test2 4/34/3 hsadkkadsh 3
如何在该表中打印2和3 ...... :(请你给我任何疑问)
答案 0 :(得分:0)
我不相信可以同步执行此操作,尝试这可能不是一个好主意。
在这种情况下,您可以获得使用子查询后的值,例如:
SELECT *,
(SELECT COUNT(*) FROM CaseTableDetail WHERE CaseTableDetail.CaseID = CaseTable.id)
AS CaseCount
FROM CaseTable;
(这只是猜测,因为您没有为CaseName表指定完整的表结构)
编辑:
要使上述工作正常,您需要一个适当的关系结构,而不是动态添加表。你应该只有2个表,我将把它们称为CaseTable和CaseDetailTable。
CaseTable正是您已经拥有的。
CaseDetailTable类似于上面的Test和Test2表,但有一个额外的字段CaseID 现在我在DB中还有两个表 测试,测试2 ..
ID CaseID DocumentName Date Notes
1 1 ppp 7/33 asdhdfkdshf
2 1 asdjhad 9/44 dfjasgfsjfj
3 2 sad 7/4 asdhdfkdshf
4 2 assd 3/44 hhhhhh
5 2 asd 2/22 adgjad
因此CaseID字段是指向CaseTable中每行所属条目的指针。使用上面使用的WHERE,JOIN和子查询,您将能够更有效地访问所有数据。您可以通过使用REFERENCES关键字告诉SQLite这是您正在做的事情。这将告诉数据库创建索引以更快地查找CaseDetails,并且它将确保您不能向CaseDetailTable添加任何行,除非您在CaseTable中有相应的条目。
您可以按如下方式创建CaseDetailTable:
CREATE TABLE CaseDetailTable (
id INTEGER PRIMARY KEY AUTOINCREMENT,
CaseID INTEGER REFERENCES CaseTable (ID),
Notes TEXT unique NOT NULL,
DocumentName INTEGER,
Date TEXT NOT NULL
);
答案 1 :(得分:0)
你可以通过做这样的事情来异步循环它们(没有经过测试,但希望你能得到这个想法):
var count = 0;
var caseTableResult = [];
var getallTableData = function (tx) {
tx.executeSql('SELECT * FROM CaseTable', [], querySuccess, errorCB);
}
var querySuccess = function (tx, result) {
if (count === 0) {
caseTableResult = result;
$('#folderData').empty();
} else {
var i = count - 1;
$('#folderData').append(
'<li class="caseRowClick" id="' + caseTableResult.rows.item(i).id + '" data-rel="popup" data-position-to="window">' + '<a href="#">' + '<img src="img/Blue-Folder.png">' + '<h2>' + caseTableResult.rows.item(i).CaseName + t+'</h2>' + '<p>' + caseTableResult.rows.item(i).TextArea + '</p>' + '<p>' + caseTableResult.rows.item(i).CaseDate + '</p>' + '<span class="ui-li-count">' + i + '</span></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>'
);
}
if (count <= caseTableResult.rows.length) {
// Call the next query
count += 1;
tx.executeSql('SELECT count(*) FROM ' + caseTableResult.rows.item(i).CaseName, [], querySuccess, errorCB);
} else {
// We're done
$('#folderData').listview('refresh');
}
}
但实际上你不应该创建大量具有相同结构和不同名称的表,你应该有一个表,其中所有数据都是通过关系连接的,那么你可以使用我的other answer。
答案 2 :(得分:0)
我喜欢这个.. !!它正在工作。谢谢你的帮助
function getallTableData(tx) {
// tx.executeSql("DROP TABLE IF EXISTS a");
tx.executeSql('SELECT * FROM CaseTable', [], querySuccess, errorCB);
}
function querySuccess(tx, result) {
var len = result.rows.length;
var countDoument=0
$('#folderData').empty();
for (var i = 0; i < len; i++) {
alert(i)
test1=result.rows.item(i).CaseName;
Test1(test1, function(i) {
return function(result_count) {
countDoument = result_count; // here it count value
alert(result_count + "result_count") //alert is correct count value
alert(i + "i");
$('#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 + countDoument + '</h2>' + '<p>' + result.rows.item(i).TextArea + '</p>' + '<p>' + result.rows.item(i).CaseDate + '</p>' +'<span class="ui-li-count">' + i + '</span>'+ '</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>' + '<span class="ui-li-count">' + i + '</span>'+ '</li>'
);
$('#folderData').listview('refresh');
};
}(i));
}
}
function Test1(test, callBack){
var x;
db.transaction(function (tx) {
$yoursql = 'SELECT * FROM "'+test+'" ';
tx.executeSql($yoursql, [], function (tx, results) {
x = results.rows.length + "TableName" + test;
callBack(x);
});
});
}