mysql.query doesnt work if i set a sleep ¿?

32 views
Skip to first unread message

Jesús TT

unread,
Oct 29, 2016, 12:08:33 PM10/29/16
to nodejs
Hi friends of the group!
I am mad
with this issue.

I cant save
in DB with connection.query() if I set a while with a pause.
Its very strange.
Any idea?
Thanks !


___________________________________________________


//npm install mysql
var mysql = require('mysql');


var connection = mysql.createConnection({
   host
: 'localhost',
   user
: 'myUser',
   password
: '******',
   database
: 'myDB',
   port
: 3306
});
connection
.connect(function(error){
   
if(error){throw error;}else{console.log('Conected to mySQL');}
});


//LOOP//
while(true){    
    console
.log("Loop!");
    save_bd
();
    sleepFor
(1000); //<------------- If remove this line the data is saved in the DB each while loop ¿¿??////////////////
}




//FUNCTIONS//
function save_bd(){
    connection
.query("UPDATE myTable SET myTime='"+new Date().getTime()+"' WHERE Id='1'", function(errors, rowss){
   
if(errors){ throw errors;}else{  
        console
.log("Why not Saved?");
   
}});//END query_read_data
}
function sleepFor( sleepDuration ){
   
var now = new Date().getTime();
   
while(new Date().getTime() < now + sleepDuration){ /* do nothing */ }
}



Reply all
Reply to author
Forward
0 new messages