简体   繁体   中英

Incorrect count value returned by the count function from the HTML browser database

I just want to count the number of rows in the database, but it seems to be showing wrong values. To be precise, it always returns 1.
The HTML code:

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
</head>
<body>
    <input type="date" id="dt">
    <input type="button" id="submit" value="INSERT DATE">
    <input type="button" id="count" value="count">
</body>
<script type="text/javascript" src="js/index.js"></script>
</html>

And the JavaScript:

var db = openDatabase("Dates", "1.0", "Test Dates", 200000);     
var createStatement = "CREATE TABLE IF NOT EXISTS Date (sampledate DATE)";    
var insertStatement = "INSERT INTO Date (sampledate) VALUES (?)";    
var countcomm = "SELECT COUNT(*) FROM Date";    
var dataset;    

db.transaction(function(xd)
          {
    xd.executeSql(createStatement, []);
});

$(document).ready(function()  
{
    $("body").fadeIn(2000);
    $("#submit").click(insertdate);
    $("#count").click(countdates);
});

function countdates()
{
db.transaction(function(xd)
              {
                xd.executeSql(countcomm, [], function (xd, result) {
                dataset = result.rows;
                alert(dataset.length);
        });
    });
}

function insertdate()
{
    var datetemp = $("#dt").val();
    db.transaction(function(xd)
              {
        xd.executeSql(insertStatement, [datetemp])
    })
    alert("SUCCESS");
}

OUTPUT

As you can see in the alert that the count value it shows is 1 whereas it is expected to show 5 since there are 5 rows in the database

Change var countcomm = "SELECT COUNT(*) FROM Date"; to var countcomm = "SELECT * FROM Date";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM