Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using MySQL with node.js

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 452
    Comment on it

    Hello Everyone , In this blog we will discuss about how to use MYSQL in node.js.

    node-mysql is one of the best modules which can be used for working with MySQL database.

    To install the MySQL in node.js driver you have to follow up with the below process:

    a) Write below code in your terminal under the node js project folder:

    npm install -g mysql
    

    b)- Adding dependencies in package.json:

    "dependencies": {
        "mysql": "~2.3.2",
         ...
    

    c)- run npm install


    In your js file add the below code:-

    var express    = require("loopback");
    var mysql      = require('mysql');
    var connection = mysql.createConnection({
      host     : '192.168.1.55',
      user     : 'root',
      password : '123456',
      database : 'paScience'
    });
    console.log(connection);
    //var app = express();
    
    connection.connect(function(err){
    if(!err) {
        console.log("Database is connected ... \n\n");  
    } else {
        console.log("Error connecting database ... \n\n");  
    }
    });
    
    
    connection.query('SELECT * from products', function(err, rows, fields) {
    connection.end();
      if (!err)
        console.log('The solution is: ', rows);
      else
        console.log('Error while performing Query.');
      });
    
    
    var data = { image_name: 'Winnie.jpeg', status: '1' };
    connection.query('INSERT INTO products SET ?', data, function(err,res){
      if(err) throw err;
    
      console.log('Last insert ID:', res.insertId);
    });
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: