tags:

views:

73

answers:

1

Ok i have this function. what it should be doing it looking for pid in sql and if found skip the function and move on, but I dont think my if statment is working right.

function getblogpost(div) {    
    var date = $(div).find('.time').text();
    var user = $(div).find('.user').text();
    var title = $(div).find('.title').text();
    var textbody = $(div).find('.bodytext').text();
    var postid = $(div).find('.pid').text();

    var q = tx.executeSql("SELECT * FROM blogpost WHERE postid=" + postid, 
                          [], function(transaction, result) {
                            var sqlpostid = result.rows.item(i)['postid'];
            }, null);
    if(result == NULL) {
        return;
    }
    else {
    dbsql.transaction(
    function(transaction) {
        transaction.executeSql(
        'INSERT INTO blogpost (postid, date, user, title, textbody) 
                    VALUES (?, ?, ?, ?, ?);', 
        [postid, date, user, title, textbody], 
        function() { },
        errorHandler
        );
    }
    );

    return false;
    }
}

Should this not work?

Thanks

Edit: what about this.

function getblogpost(div) {   
    var postid = $(div).find('.pid').text();
    var q = tx.executeSql("SELECT * FROM blogpost WHERE postid=" + postid, [], function(transaction));
    if(!q) {
        return false;
    }

    var date = $(div).find('.time').text();
    var user = $(div).find('.user').text();
    var title = $(div).find('.title').text();
    var textbody = $(div).find('.bodytext').text();

        dbsql.transaction(
            function(transaction) {
                transaction.executeSql(
                'INSERT INTO blogpost (postid, date, user, title, textbody) VALUES (?, ?, ?, ?, ?);', 
                [postid, date, user, title, textbody], 
                function() { }, 
                errorHandler
                );
            }
        );      
    return false;       
}

What i'm trying to do here is find out if postid is in the database and if so skip the function and return.

+2  A: 

Well you need to define NULL somewhere like

var NULL = null;

Without that, NULL is undefined in ECMA-/Javascript.

Anyways, why would you do that? executeSql() takes two Callbacks as parameters. The second one is for an error case, which fits probably better.

t.executeSql('SELECT Foobar FROM blogpost', [], function (t, r) {
  // r.rows[1].Something
}, function (t, e) {
  console.log(e.message);
});     

For instance.

jAndy
Thanks, jAndy, now I can delete my answer. +1.
paxdiablo