Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

how to avoid redundency while inserting into the database in scrapping using nodejs and mysql

how to avoid redundency while inserting into the database in scrapping using nodejs and mysql

Problem

Am developing one using nodejs scrapping and mysql. I want to store details into the mysql Database. I have written the query it is saving sucessfully and it also checks for redundancy if we run the script again. If there is no data (empty table) in the the table then, it is not checking for redundancy. All data is saving into the database.

The code what i have written to avoid redundency and insert into the table.

mysql.query(
    'select id, name from ' + product_details + ' where name = "'+$name+'"',                                                             
    function(err, result, fields) {
        if (err) throw err;
        else {
            var i;
            for (i in result) {
                var product = result[i];
                if(product.id>0){
                }
            }
            if(i==undefined){
                mysql.query('insert into '+ product_details +' (name) values ("' + $name + '")',
                    function selectCb(err, results, fields) {
                        if (err) throw err;
                        else {
                            console.log("inserted");
                    }
            });
        }
    }
});

Please help me to come out from this problem. Am new to nodejs scrapping. Please help me. Thanks in advance. ** UPDATE **

Thank you user1027167. I tried your solution and i fixed the problem. But now mysql error occurs then script execution stops, but i want script to continue its execution by omitting (leaving) that detail to be saved in the database. How to do that please help me

Problem courtesy of: user1074042

Solution

you have to fetch the error like this:

mysql.query('insert into '+ product_details +' (name) values ("' + $name + '")',
    function errorhandler(err, results, fields) {
        if (err)
        {
            console.log("name is not unique");                
            // do something else
        }
    }
});

So you should not throw it.

Solution courtesy of: user1027167

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

how to avoid redundency while inserting into the database in scrapping using nodejs and mysql

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×