Last active
March 22, 2023 07:45
-
-
Save smching/c3755ff035ca6fbf17da4cac56b20bc2 to your computer and use it in GitHub Desktop.
Node.js application: Insert a row to a table in MySQL database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where is the db file or is it connect with xammp??