MySQL 如何使用node-mysql将MySql查询的结果保存在变量中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26100184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save the result of MySql query in variable using node-mysql
提问by Fadi
im trying to save the result of MySql query in variable using node-mysql model and node.js so i have this code:
我试图使用 node-mysql 模型和 node.js 将 MySql 查询的结果保存在变量中,所以我有这个代码:
connection.query("select * from ROOMS", function(err, rows){
if(err) {
throw err;
} else {
console.log(rows);
}
});
and the result is :
结果是:
[ { idRooms: 1, Room_Name: 'dd' },
{ idRooms: 2, Room_Name: 'sad' } ]
so i need to store this results in variable so i try like this:
所以我需要将这个结果存储在变量中,所以我尝试这样:
var someVar = connection.query("select * from ROOMS", function(err, rows){
if(err) {
throw err;
} else {
return rows;
}
});
console.log(someVar);
but is not working thanks for any help in advance.
但不工作感谢提前提供任何帮助。
回答by Rodrigo Medeiros
Well @Fadi, connection.query
is async, which mean when your call your console.log(someVar)
, someVar
has not been set yet.
好吧@Fadi,connection.query
是异步的,这意味着当您调用您的console.log(someVar)
, 时someVar
尚未设置。
What you could do:
你可以做什么:
var someVar = [];
connection.query("select * from ROOMS", function(err, rows){
if(err) {
throw err;
} else {
setValue(rows);
}
});
function setValue(value) {
someVar = value;
console.log(someVar);
}
回答by Geery.S
As a complement to already given answers.
作为对已经给出答案的补充。
var **someVar** = connection.query( *sqlQuery*, *callback function( err , row , fields){}* )
console.log(**someVar**);
This construction will return in someVarinformation of this connection and his SQL query . It will not return values ??from the query .
此构造将返回此连接及其 SQL 查询的someVar信息。它不会从查询中返回值。
Values ??from the query are located in the callback function ( err , row , fields)
来自查询的值位于回调函数中( err 、 row 、 fields )
回答by mscdex
You can't do that because network i/o is asynchronous and non-blocking in node.js. So any logic that comes afterwards that must execute only after the query has finished, you must place insidethe query's callback. If you have many nested asynchronous operations you may look into using a module such as async
to help better organize your asynchronous tasks.
你不能这样做,因为网络 i/o 在 node.js 中是异步和非阻塞的。所以自带事后查询结束后只能执行任何逻辑,你必须把里面查询的回调。如果您有许多嵌套的异步操作,您可以考虑使用一个模块async
来帮助更好地组织您的异步任务。
回答by G?kalp Turan
Here is your answer if you want to assign a variable to res.render
如果您想为 res.render 分配一个变量,这是您的答案
//before define the values
var tum_render = [];
tum_render.title = "Turan";
tum_render.description = "Turan'?n websitesi";
//left the queries seperatly
var rastgeleQuery = "SELECT baslik,hit FROM icerik ORDER BY RAND() LIMIT 1";
var son5Query = "SELECT baslik,hit FROM icerik LIMIT 5";
var query_arr = [rastgeleQuery, son5Query];
var query_name = ['rastgele', 'son5'];
//and the functions are
//query for db
function runQuery(query_arr,sira){
connection.query(query_arr[sira], function(err, rows, fields) {
if (err) throw err;
var obj = [];
obj[query_name[sira]] = rows;
sonuclar.push(obj);
if (query_arr.length <= sira+1){
sonuc();
}else{
runQuery(query_arr, sira+1);
}
});
}
//the function joins some functions http://stackoverflow.com/questions/2454295/javascript-concatenate-properties-from-multiple-objects-associative-array
function collect() {
var ret = {};
var len = arguments.length;
for (var i=0; i<len; i++) {
for (p in arguments[i]) {
if (arguments[i].hasOwnProperty(p)) {
ret[p] = arguments[i][p];
}
}
}
return ret;
}
//runQuery callback
function sonuc(){
for(var x = 0; x<=sonuclar.length-1; x++){
tum_render = collect(tum_render,sonuclar[x]);
}
console.log(tum_render);
res.render('index', tum_render);
}