Matt Mateo
Matt Mateo

Reputation: 168

SQLite count and get other records in Cordova

Snippet of my code:

var sql = "SELECT name,data, COUNT(name) as 'mycount' FROM floodMaps WHERE name='?' GROUP BY name,data";
             db.transaction(function(tx) {
                tx.executeSql(sql,[flodMapName], function(tx,res){  
                    //console.log(res.rows.item(0).data);
                     console.log(res.rows.item(0).mycount);
                    if(res.rows.item(0).mycount > 0) {
                        console.log('GETTING DATA FROM DB...');

It throws an error:

a statement with no error handler failed: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.

I am trying to count at the same time get the data from a table but unfortunately it's not working. I did my research, I haven't found a good documentation for the plugin.

This is how I created the table:

document.addEventListener("deviceready", function(){  
    //database
    db = window.sqlitePlugin.openDatabase({name: 'demo.db', location: 'default'}); 
    db.transaction(function(tx) {
        tx.executeSql('CREATE TABLE IF NOT EXISTS floodMaps (name text primary key, data text)');
        tx.executeSql('DELETE FROM floodMaps');
    }, function(error) {
        console.log('Create Table ERROR: ' + error.message);
    }, function() {
        console.log('Table created successfully.');
    });
    //end database
}, false);

The whole code is here.

Upvotes: 0

Views: 662

Answers (2)

Gandhi
Gandhi

Reputation: 11935

I guess you need not have to use single quote at all for alias. You can use as follows:

SELECT name,data, COUNT(name) as mycount FROM floodMaps WHERE name=?

Check out this official link that has sample for count query too.

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521639

I believe that the ? parameter placeholder should not be in single quotes. Try this instead:

var sql = "SELECT name, data, COUNT(name) as `mycount` FROM floodMaps WHERE name= ? GROUP BY name, data";

Upvotes: 2

Related Questions