javascript 以 HTML 格式显示 SQL 查询结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14164324/
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
Display SQL Query Result in HTML
提问by EzAnalyst
I need to use javascript and HTML5 to display the result of the following SQL query in my html page. The SQL query works in SQLite Browser, but I am unsure of how to write the function and corresponding HTML5 code to call the function to display the result of my query. The SQL Query is as follows:
我需要使用 javascript 和 HTML5 在我的 html 页面中显示以下 SQL 查询的结果。SQL 查询在 SQLite 浏览器中工作,但我不确定如何编写该函数和相应的 HTML5 代码来调用该函数以显示我的查询结果。SQL查询如下:
SELECT SUM(Orders.productQty * Products.productPrice) AS grandTotal FROM Orders JOIN Products ON Products.productID = Orders.productID
This returns a numerical result from my SQLite database that is already created, but I do not get how to display the result of the select query on my webpage.
这会从我已经创建的 SQLite 数据库返回一个数字结果,但我不知道如何在我的网页上显示选择查询的结果。
I've tried using the following function to execute the sql statement, but I do not know how to display it using HTML.
我已经尝试使用以下函数来执行 sql 语句,但我不知道如何使用 HTML 来显示它。
function calculateTotalDue() {
db.transaction(function (tx) {
tx.executeSql('SELECT SUM(Orders.productQty * Products.productPrice) AS grandTotal FROM Orders JOIN Products ON Products.productID = Orders.productID', [], []);
});
}
}
Would someone please show me how to display the result of the query in my html page?
有人可以告诉我如何在我的 html 页面中显示查询结果吗?
回答by jsweazy
What you need is a function in the third parameter of the executeSql call. like this ( this is an example if you have mulitple results, but will work with your query too ):
您需要的是 executeSql 调用的第三个参数中的函数。像这样(如果您有多个结果,这是一个示例,但也可以处理您的查询):
Javascript
Javascript
function calculateTotalDue() {
db.transaction(function (tx) {
tx.executeSql('SELECT SUM(Orders.productQty * Products.productPrice) AS grandTotal FROM Orders JOIN Products ON Products.productID = Orders.productID', [],
function(){
// Get return rows
var data = result.rows;
// Initialize variable to store your html
var html = '';
// loop thru results
for (var i = 0; i < dataset.length; i++) {
var row = data.item(i);
// Add to html variable
html += row.grandTotal;
// Append that html somewhere
// How todo this will vary depening on if you are using framworks or not
// If just javascript use:
// document.getElementById('results').innerHTML += html;
}
}
);
});
}