0

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

1
  • 2
    You need to look at the value in the row. Any count() query with no group by is always going to return one row. Commented Jul 27, 2018 at 16:42

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.