Razgriz
Razgriz

Reputation: 7343

Javascript - Function to Count Unique Database Rows

I am writing a Javascript function where I need to count the number of unique entry in a certain column in my SQLite Database. So far, what I have tried to do is to just simply count the number of entries present as I need that as a parameter when I populate entries. My function goes as such:

MasterTreeDB.transaction(function(transaction) {
    transaction.executeSql("SELECT COUNT(*) FROM MasterTreeTable", [],
        function(transaction, result) {
            //console.log(result.rows);
            console.log(result.rows.item(0));
        },
        function(transaction, error){
            // error occured
            console.log("Error occured");

        }
    );
});

However, this is what the console shows: Object {COUNT(*): 4}

I have also looked around and I saw a solution that involved changing results.row.item(0) to results.row.item(0)["count(*)"]. However, when I did that, the log showed undefined

I feel like I'm really close to getting the result I want and I'm missing something rather basic. Any help/input is very much appreciated, thank you in advance.

Upvotes: 0

Views: 899

Answers (1)

David-SkyMesh
David-SkyMesh

Reputation: 5171

"count the number of unique entry in a certain column in my SQLite Database":

select TheColumn from MasterTreeTable group by TheColumn;

Then result.rows.length is the number of unique values contained in TheColumn.

Upvotes: 1

Related Questions