I'm trying to insert some data into my mysql database using a loop. As you can see, I open/insert/close the connection once per cycle. However, it seems that my code isn't reopening my database connection after a loop.
Source Code:
var MySQLModule = require('mysql');
//var SleepModule = require('sleep');
var MySQL_Connection = MySQLModule.createConnection({host: '10.0.0.254', user: 'User', password: 'Password', database: 'Air_Pollution_Project'});
var i = 0;
while (i < 5) {
MySQL_Connection.connect();
//SleepModule.sleep(0);
MySQL_Connection.query('INSERT INTO Air_Pollution_Reading_Record (Station_ID, Air_Pollution_Reading_Value) VALUES (2, 666)');
MySQL_Connection.end();
i++
};
Result:
events.js:183
throw er; // Unhandled 'error' event
^
Error: Cannot enqueue Handshake after invoking quit.
Am I opening the connection correctly? What should I do.