0
con.connect(function(err){
if(err) throw err;
console.log("Connected!");
var sql = "USE test; INSERT INTO emails (FirstName, Email) VALUES ('Gaz', 'gmail');";
con.query(sql, function(err, result){
    if(err) throw err;
    console.log("Row inserted at position");
})

});

I'm trying to use node.js to insert a new row in an already existing table in MySQL. I connect properly but then an error is thrown in the console log which says the SQL syntax is wrong but I have checked and the sytax set for the sql variable works fine in SQL. The .query(sql, function()) format seems to be correct according to w3schools and other sources. I can't tell what the problem is.

goduffy
  • 1
  • 1
  • Change the sql code into `INSERT INTO test.emails (FirstName, Email) VALUES ('Gaz', 'gmail')` i think it's not possible to use multiple sql statements in the node.js query function... edit iám right.. https://github.com/mysqljs/mysql#multiple-statement-queries – Raymond Nijland Sep 06 '17 at 16:34

1 Answers1

0

According to this answer you should enable multiple statements option when creating connection to be able to use multiple statements. check the answer for more information.

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  multipleStatements: true
});
Javad
  • 437
  • 1
  • 5
  • 14