javascript SQLite 选择语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5136886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLite Select Statement
提问by mao
can someone help me with what I think is probably a simple problem please. I think the problem is probably with my SQL select statement, but maybe not.
有人可以帮我解决我认为可能很简单的问题吗?我认为问题可能出在我的 SQL select 语句上,但也可能不是。
I have a table named tableOne with two columns, name and total, the function editRecord is called and the id of the row in question is passed to it so I can select this row, then editRecords2 function is called. This second function generates a form in the html and adds the name and total values from the particular row in the table as into the form boxes.
我有一个名为 tableOne 的表,有两列,name 和 total,调用函数 editRecord 并将相关行的 id 传递给它,以便我可以选择这一行,然后调用 editRecords2 函数。第二个函数在 html 中生成一个表单,并将表中特定行的名称和总值添加到表单框中。
The problem is that the values from the desired row never appear in the form, always the values from the last row go into the form. The code is below, any help would be great, thanks!
问题是来自所需行的值永远不会出现在表单中,最后一行的值总是进入表单。代码如下,任何帮助都会很棒,谢谢!
function editRecord(id) {
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM tableOne WHERE id=?', [id], editRecords2);
});
}
function editRecords2() {
f = $('#edit');
f.html("");
f.html(f.html() + ' <form method="get" id="edit_form"><div><input type="name" id="editname" value="' + r['name']+'" size="30"/><input type="number" current id="editamount" value="' + r['total']+'" name="amount" size="15" /><input type="submit" value="Save" /></div></form> ');
}
回答by Sujeet
I haven't tested the code yet but this will help you to get your output
我还没有测试过代码,但这会帮助你得到你的输出
function editRecord(id) {
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM tableOne WHERE id=?', [id], function(tx,results){
for (var i=0; i < results.rows.length; i++){
row = results.rows.item(i);
editRecords2(row.name,row.total);
}
});
});
}
function editRecords2(name,total) {
f = $('#edit');
f.html("");
f.html(f.html() + ' <form method="get" id="edit_form"><div><input type="name" id="editname" value="' + name+'" size="30"/><input type="number" current id="editamount" value="' + total+'" name="amount" size="15" /><input type="submit" value="Save" /></div></form> ');
}
if any problem occure contact me.
如果出现任何问题,请与我联系。
回答by Marc Bernier
I think what you need to look at is SQLite's User Defined Functions. You want SQL to call your function for each record in the table. This must be done at the SQL level, and not the JavaScript level. In C++, it's a piece of cake, SQLite has a sqlite_create_function() that will allow you to do this. I'm not sure if there's one for JavaScript, but that's what you'll need to look for.
我认为您需要查看的是 SQLite 的用户定义函数。您希望 SQL 为表中的每条记录调用您的函数。这必须在 SQL 级别完成,而不是在 JavaScript 级别。在 C++ 中,这是小菜一碟,SQLite 有一个 sqlite_create_function() 可以让你做到这一点。我不确定是否有适用于 JavaScript 的版本,但这就是您需要查找的内容。
回答by msmafra
SQLite looks a little tricky. But it seems that if try to access it without a server side language you won't have success
SQLite 看起来有点棘手。但似乎如果尝试在没有服务器端语言的情况下访问它,您将不会成功

