Skip to content

Instantly share code, notes, and snippets.

Last active Jul 2, 2017
What would you like to do?
Node.js application: Insert a row to a table in MySQL database
var mysql = require('mysql');
//Create Connection
var connection = mysql.createConnection({
host: "",
user: "newuser",
password: "mypassword",
database: "mydb"
connection.connect(function(err) {
if (err) throw err;
console.log("Database Connected!");
//insert a row into the tbl_messages table
connection.connect(function(err) {
var clientID= "client001";
var topic = "myhome/kitchen";
var message = "dev01,on";
var sql = "INSERT INTO ?? (??,??,??) VALUES (?,?,?)";
var params = ['tbl_messages', 'clientID', 'topic', 'message', clientID, topic, message];
sql = mysql.format(sql, params);
connection.query(sql, function (error, results) {
if (error) throw error;
console.log("1 record inserted");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment