nodejs 连接MySQL后,输出数据带有RowDataPacket、中括号大括号怎么去掉?

 

var mysql  = require('mysql');  
 
var connection = mysql.createConnection({     
  host     : 'localhost',       
  user     : 'root',              
  password : '123456',       
  port: '3306',                   
  database: 'reji' 
}); 
 
connection.connect();
 
var  sql = 'SELECT * FROM user';
//查
connection.query(sql,function (err, result) {
        if(err){
          console.log('[SELECT ERROR] - ',err.message);
          return;
        }
 
       console.log('--------------------------SELECT----------------------------');
       console.log(result);
       console.log('------------------------------------------------------------\n\n');  
});
 
connection.end();

  运行结果如下:

如果单纯是查询结果那这样显示也是没有问题的,但是如果我们是想把数据传送出去或者对应读取出数据就 不太好操作了

 

我们进行如下更改代码:

var mysql  = require('mysql');  
 
var connection = mysql.createConnection({     
  host     : 'localhost',       
  user     : 'root',              
  password : '123456',       
  port: '3306',                   
  database: 'reji' 
}); 
 
connection.connect();
 
var  sql = 'SELECT * FROM user';
//查
connection.query(sql,function (err, result) {
        if(err){
          console.log('[SELECT ERROR] - ',err.message);
          return;
        }
 
       console.log('--------------------------SELECT----------------------------');
       console.log('account:'result[0].account);//更改位置
       console.log('------------------------------------------------------------\n\n');  
});
 
connection.end();

  运行结果:

 

 用例二:查询特定帐号的name值

'use strict';
var app = require('express')();
var express = require("express");
var server = require('http').Server(app);
var io = require('socket.io')(server);
var mysql  = require('mysql');  
 
var connection = mysql.createConnection({     
  host     : 'localhost',       
  user     : 'root',              
  password : '123456',       
  port: '3306',                   
  database: 'reji' 
}); 
 
connection.connect();



//var name='select name form user where account=1870096555';
var name = "select name from user where account='"+1870096555+"'";
connection.query(name,function (err, results) {
        if(err){
          console.log('[SELECT ERROR] - ',err.message);
          return;
        }
		//results = JSON.stringify(results);
		//var temp=new String(results[0].name);
       console.log('--------------------------SELECT----------------------------');
       console.log('name:',results[0].name);
       console.log('------------------------------------------------------------\n\n');  
});
//console.log(name);


connection.end();

  

 

posted @ 2018-11-22 17:34  cs_wu  阅读(2530)  评论(0编辑  收藏  举报