Skip to content Skip to sidebar Skip to footer

How To Check If Data Already Exists Before Inserting On A Table On Html5 Javascript Sqlite?

I'm developing a web application that grabs a website RSS feed and save it into a database, using HTML5/Javascript sqlite. This is my code function createPosts(title,author,content

Solution 1:

how about refactoring checkifExists to take two callbacks and use a where clause to the sql rather then doing the search in javascript so

functioncheckIfExists(publishedDate,callbacktrue,callbackfalse){
    var query = "SELECT * FROM mytable WHERE data=?;";
    try {
        localDB.transaction(function(transaction){
            transaction.executeSql(query, [publishedDate], function(transaction, results){
                if(results.rows.length > 0){
                    callbacktrue();
                } else {
                    callbackfalse();
                }
            }, function(transaction, result){
                    callbacktrue();
            }, function(transaction, error){
                updateStatus("Erro: " + error.code + "<br>Mensagem: " + error.message);
                    callbacktrue();
            });

        });
    } 
    catch (e) {
        updateStatus("Error: SELECT não realizado " + e + ".");
        callbacktrue();
    }
}
functioncreatePosts(title,author,content,contentSnippet,publishedDate,img_destaque){
    checkIfExists(publishedDate,function(){
            updateStatus("Error: Post already exists");
        },function(){
            var query = "insert into mytable (titulo, data, autor, img_destaque,descricao,postagem) VALUES (?, ?, ?, ?, ?, ?);";
            try {
                localDB.transaction(function(transaction){
                    transaction.executeSql(query, [title, publishedDate, author, img_destaque, contentSnippet, content], function(transaction, results){
                        if (!results.rowsAffected) {
                            updateStatus("Error, not data inserted");
                        }
                        else {
                            updateStatus("Data inserted, id: " + results.insertId);
                        }
                    }, errorHandler);
                });
            } 
            catch (e) {
                updateStatus("Error: " + e + ".");
            }
    });               
}

Solution 2:

can use:

<inputtype="text"name="name"class="text_cs"><spanclass="result"></span></br>
Data:<spanclass="data-id">123,43,52,15</span>

and javascript:

$('.text_cs').on('change', function(){
var pid = $(this).val();
var table = $('.data-id');
if(table.html().split(",").indexOf(pid) != -1) {
   $('.result').html("ID exist");
}
   else {
       $('.result').html("Not exist");
   }

});

Post a Comment for "How To Check If Data Already Exists Before Inserting On A Table On Html5 Javascript Sqlite?"