Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing the function countRows #16

Open
davidmgallardo opened this issue Sep 2, 2015 · 0 comments
Open

Changing the function countRows #16

davidmgallardo opened this issue Sep 2, 2015 · 0 comments

Comments

@davidmgallardo
Copy link

I modified the countRows function to work properly, as in the js is a field that is called ID which I have in the database, and if not on all tables to be set up would require a field that is called by that way. With this change does not predisposed anyone to create a field with that name, but you can create any field and can pass as a parameter to the function countRows. As you can see in the code I added a new parameter to JSON (field) that receive the value of the field that we have to our table and we want to make the counter.

dbhelper.prototype.countRows = function (obj, callback) {
    //var sql = 'SELECT COUNT(id) as counter FROM ' + obj.table;
    var sql = 'SELECT COUNT(' + obj.field + ') as counter FROM ' + obj.table;
    if (obj.joiner) {
        sql = 'SELECT COUNT(' + obj.joiner + '.id) as counter FROM ' + obj.table;
    }
    if (obj.where) {
        sql += ' WHERE ' + obj.where;
    }
    Ti.API.info(sql);
    var result = this.getData(sql);
    this.db.close();

    if (callback) {
        callback(result[0].counter);
    } else {
        return result[0].counter;
    }
};

The function call would in this way:

var total=db.countRows({
        field: 'idAviso',
        table:  'avisos',
        where:   "aviso='" + mensajeAviso + "' and tipoAviso='" + tipo + "' and fechaAviso='" + fecha + "'"
    });

I hope I explained well, and can help a little to improve this fantastic tool.
A greeting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant