nodejs 連接MySQL後,輸出數據帶有RowDataPacket、中括號大括號怎麽去掉?
阿新 • • 發佈:2018-11-22
connect 沒有 temp 去掉 我們 highlight src exp png
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、中括號大括號怎麽去掉?