最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Display SQL Query Result in HTML - Stack Overflow

programmeradmin2浏览0评论

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:

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.

I've tried using the following function to execute the sql statement, but I do not know how to display it using 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?

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:

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.

I've tried using the following function to execute the sql statement, but I do not know how to display it using 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?

Share Improve this question asked Jan 4, 2013 at 20:20 EzAnalystEzAnalyst 2533 gold badges4 silver badges12 bronze badges 2
  • Do you mean display the results of your query? – jsweazy Commented Jan 4, 2013 at 20:30
  • Yes. Display the result on my html page, but there should only be one result, the sum. – EzAnalyst Commented Jan 4, 2013 at 20:38
Add a ment  | 

1 Answer 1

Reset to default 1

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 ):

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;


        }

      }
    );
  });
}
发布评论

评论列表(0)

  1. 暂无评论