Search code examples
javascripttitaniumtitanium-mobile

Titanium error in retrieving rows


getting error when retrieving the datas from the row using an id and displaying it in the textfield in next page..and another error is all the rows are getting deleted when passing id for a specific row..

Here is the coding:

var data = [];

var db = Titanium.Database.open('trip');

db.execute('CREATE TABLE IF NOT EXISTS newtrip (id INTEGER PRIMARY KEY AUTOINCREMENT, triplabel TEXT,tripname TEXT,destination TEXT,fromdate TEXT,todate TEXT)');
//db.execute('INSERT INTO newtrip(triplabel,tripname,destination,fromdate,todate) VALUES(?,?,?,?,?)',"British museum","mytrip","london","12-10-2014","12-12-2014");
//db.execute('DELETE FROM newtrip');
var resultrows = db.execute('SELECT destination,fromdate,todate FROM newtrip');

while (resultrows.isValidRow()) {

    //var res=
    var row = Ti.UI.createTableViewRow({
        height : Ti.UI.SIZE,
        rightImage : '/images/right1.png',

        layout : 'absolute'
    });

    var tripnamelabel = Ti.UI.createLabel({
        //text : 'Buckingham Palace',
        text : resultrows.fieldByName('destination'),
        color : theme_style,
        font : {
            fontSize : '16dp',
            fontWeight : 'bold'
        },

        top : '10dp',
        left : '10dp',
        //right:'30dp'
    });

    var gettablecount = resultrows.rowCount;
    for (var i = 0; i < gettablecount; i++) {
        var data_edit = [];
        var imgedit = Ti.UI.createButton({

            backgroundImage : '/images/list_edit.png',
            // left:'200dp',
            top : '20dp',
            width : wb,
            height : hb,
            // bottom:10,
            right : '20dp',
            onClick : "edit",
            rowid : resultrows.fieldByName('id')
        });
            if (resultrows.isValidRow()) {
                imgedit.addEventListener('click', function(e) {
                    var db = Titanium.Database.open('trip');

                    if (e.source.onClick == "edit") {
                        var x = db.execute('SELECT * FROM newtrip WHERE id=' + rowid);
                        //alert(x);
                        var createnewWindowback = require('ui/apppage5');
                        //the name of the url you wish to move
                        new createnewWindowback(e.source.rowid).open();
                        win.close();
                    }
                    resultrows.close();
                    db.close();

                });

        }

    }

    for (var i = 0; i < gettablecount; i++) {

        var imgdelete = Ti.UI.createButton({

            backgroundImage : '/images/delete_ic.png',
            // left:'240dp',
            top : '20dp',
            width : wb,
            height : hb,
            //bottom:'20dp',
            right : '60dp',
            onClick : "delete",
            rowid : resultrows.fieldByName('id')
        });

        imgdelete.addEventListener('click', function(e) {
            var db = Titanium.Database.open('trip');

            if (e.source.onClick == "delete") {
                var x = db.execute('DELETE FROM newtrip WHERE id=' + rowid);
                alert("you have just clicked the delete button");
            }
            resultrows.next();
            db.close();

        });
    }
    var fromdate = Ti.UI.createLabel({
        //text : '10.11.2014',
        text : resultrows.fieldByName('fromdate'),
        color : 'Black',
        font : {
            fontSize : '13dp',
            fontWeight : 'bold'
        },
        top : '40dp',
        left : '10dp',
        right : '10dp',
        bottom : '20dp'
    });

    var dash = Ti.UI.createLabel({
        text : '-',
        color : 'Black',
        font : {
            fontSize : '15dp',
            fontWeight : 'bold'
        },
        top : '40dp',
        left : '80dp',
        right : '10dp',
        bottom : '20dp'
    });

    var todate = Ti.UI.createLabel({

        text : resultrows.fieldByName('todate'),
        color : 'Black',
        font : {
            fontSize : '13dp',
            fontWeight : 'bold'
        },
        top : '40dp',
        left : '90dp',
        right : '10dp',
        bottom : '20dp'
    });
    row.add(tripnamelabel);
    row.add(imgedit);
    row.add(imgdelete);
    row.add(fromdate);
    row.add(dash);
    row.add(todate);
    row.className = 'control';
    data.push(row);
    resultrows.next();
}

resultrows.close();
db.close();
triplistview.setData(data);

Solution

  • I think you not able to fetch data according to id field because you fetch the data in resultrows variable as :

    var resultrows = db.execute('SELECT destination,fromdate,todate FROM newtrip');
    

    So here you do not fetch the id column. But you use it at :

    rowid : resultrows.fieldByName('id') // in : var imgedit
    

    Hence rowid for var imgedit would be null/undefined. You should modify your SELECT query as :

    var resultrows = db.execute('SELECT id,destination,fromdate,todate FROM newtrip');
    

    Edit : Under click listener of imgedit you have :

    var x = db.execute('SELECT * FROM newtrip WHERE id=' + rowid);
    

    But there is no variable rowid defined, hence error is thrown. Make following changes ( rowid changed to e.source.rowid) :

    var x = db.execute('SELECT * FROM newtrip WHERE id=' + e.source.rowid);
    

    Hope it helps.