nodejs 连接MySQL后,输出数据带有RowDataPacket中括号大括号怎么去掉?
Posted cs_wu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
以上是关于nodejs 连接MySQL后,输出数据带有RowDataPacket中括号大括号怎么去掉?的主要内容,如果未能解决你的问题,请参考以下文章
带有mysql的NodeJS rest api无法迭代输出格式
用php连接数据库后获取所有schema名字,为啥结果会是“Array, Array, Array, Array, Array,”