insertData.js:-
const mysql= require(‘mysql2’)
const insert = {
name: process.argv[2],
age: parseInt(process.argv[3]),
city: process.argv[4]
}
const connection = mysql.createConnection({
host: ‘localhost’ ,
database: ‘mytestdb’,
user: ‘myuser’,
password: ‘mypass’
})
connection.query(
INSERT INTO persons (name, age, city) VALUES ( '${insert.name}' , '${insert.age}' , '${insert.city}' )
,
function(err, results){
if (err){
console.error(err)
}
else{
console.log(results)
console.log("Inserted successfully")
}
connection.close();
}
)
Output:-
Deeptis-MacBook-Air:Express with SQL deeptisharma$ node .\mysql_scripts\insertData.js xyz 11 Delhi
internal/modules/cjs/loader.js:969
throw err;
^
Error: Cannot find module ‘/Users/deeptisharma/Desktop/Express with SQL/.mysql_scriptsinsertData.js’
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)
at Function.Module._load (internal/modules/cjs/loader.js:842:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
code: ‘MODULE_NOT_FOUND’,
requireStack: []
}