Skip to content

Instantly share code, notes, and snippets.

@smching
Last active March 22, 2023 07:45
Show Gist options
  • Save smching/c3755ff035ca6fbf17da4cac56b20bc2 to your computer and use it in GitHub Desktop.
Save smching/c3755ff035ca6fbf17da4cac56b20bc2 to your computer and use it in GitHub Desktop.
Node.js application: Insert a row to a table in MySQL database
var mysql = require('mysql');
//Create Connection
var connection = mysql.createConnection({
host: "192.168.1.123",
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");
});
});
@Muhammad-Hassan-Rafiq
Copy link

where is the db file or is it connect with xammp??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment