Hussian Shaik
Hussian Shaik

Reputation: 2659

How to get the data from database in node.js?

I want to get the data from database. I tried like this but what happening here is that, I am getting the total number rows. But I want the total data. Can any one help me?

connection.on('connect',function(err){
 if(err){
      console.log(err)
 }else{
 var sql = "SELECT * FROM xxxxx";
   var request = new Request(sql,
   function(err,result){
       if(err){
           console.log(err);
       }else{
           console.log("hello:"+result)
       }
   });
   connection.execSql(request);
 } 
 });

I need the total data from database.

Upvotes: 1

Views: 77

Answers (1)

Beto López
Beto López

Reputation: 91

You can do it, using pool, like this:

var mysql      = require('mysql');
var pool = mysql.createPool({
    host     : 'localhost',
    user     : 'mysql username',
    password : 'password',
    database: 'database_name'
});

pool.getConnection(function(err, connection) {
    connection.query('SELECT * FROM table',function(err,rows){
        if (err) console.log('error!: '+err);
        else console.log(rows);
    } );        
connection.release();
// Don't use the connection here, it has been returned to the pool.
});

Hope it helps

Upvotes: 2

Related Questions