Search code examples
node.jsnode-mysql

Inserting Data with Node.js


I 'am trying to insert some data with Node.js. I installed mysql support with npm . I just checked arround some source code, I've wrote following code , I can follow sql output in console.log and SQL output is correct. But It does not affect on any rows in mySQL database.

Here is my code :

var mysql      = require('mysql');

var connection = mysql.createConnection({
  host     : 'cccc.net',
  user     : 'username',
  password : 'password',
});

var post  = {srcUserID: userSrcID, destUserID: msg.userid, messageContent: msg.txt, messageSendDate:sendDate };

connection.query('INSERT INTO messages VALUES ?', post, function(err, result) {

});

Solution

  • You have to select a DB before performing a query. The easiest way is to add it to the object in the createConnection call:

    var connection = mysql.createConnection({
      host     : 'cccc.net',
      user     : 'xxxxx_usr',
      password : 'xxxxxxx',
      database : 'database_name'
    });