使用节点js将MySql查询的结果保存在变量中

时间:2016-08-18 09:17:25

标签: mysql node.js save exec

我想知道如何使用节点js

在变量中保存MySql查询的结果

我用它来连接mysql并获取查询

var mysql = require('mysql');
var express = require('express');
var app = express();


app.get('/informations', function (req, res) {

    var connection = mysql.createConnection(
        {
            host     : 'localhost',
            user     : 'root',
            password : '',
            database : 'dbUsers',
        }
    );

    connection.connect();

    var queryString = 'SELECT * FROM hpform';

    connection.query(queryString, function(err, rows, fields) {
        res.json(rows);


        for (var i in rows) {
            console.log('NomBase: ', rows[i].NomBase);
            console.log('CheminHP: ', rows[i].CheminHP);
            console.log('Chemin: ', rows[i].Chemin);
            console.log('HPuser: ', rows[i].HPuser);
            console.log('pass: ', rows[i].pass);
            console.log('path_pub: ', rows[i].path_pub);
        }
    });
    connection.end();
});
module.exports = app;

我的结果是:

[{"HPId":16,"NomBase":"Base","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"P:\\\\EXSTREAM\\\\BASES\\\\MACSF_DB_EditiqueV9.5.accdb","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"},{"HPId":21,"NomBase":"LMG","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"D:\\\\hp_LMG\\\\BaseAccess20160330.accdb","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"},{"HPId":22,"NomBase":"Meriem","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"D:\\base_meriem\\Test.accdb","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"},{"HPId":24,"NomBase":"bouygues","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"D:\\\\hp_LMG\\\\bouygues\\\\20160425 - PRODUCTION MEP FE_22606 - Ano314002 Libellé remboursement EDP Appro","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"},{"HPId":26,"NomBase":"hamza","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"P:\\\\EXSTREAM\\\\BASES\\\\MACSF_DB_EditiqueV9.5.accdb","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"}]

现在我想保存我的结果作为在exec命令中使用它们的请求

child = exec("\"" + req.session.CheminHP + "/Packager.exe\" -APPLICATION=" + req.params.app + " -ACCESSDB=" + req.session.Chemin + " -EXSTREAMUSER=" + req.session.HPuser + " -EXSTREAMPASSWORD=" + req.session.pass + "  -PACKAGEFILE=" + req.session.path_pub + "\\" + req.params.app + ".pub", function (error, stdout, stderr) 

我可以建议这样做吗? 谢谢。

2 个答案:

答案 0 :(得分:0)

如果您只从查询中获得一行,请尝试以下操作:

img {
    display: table;
    margin: auto;
}

答案 1 :(得分:0)

非常感谢Nikita Namestnikov。 我用你告诉我做的事情,然后添加

console.log('NomBase: ' + req.session.NomBase);
        console.log('CheminHP: ' + req.session.CheminHPBase);
        console.log('Chemin: ' + req.session.CheminBase);
        console.log('HPuser: ' + req.session.HPuserBase);
        console.log('pass: ' + req.session.passBase);
        console.log('path_pub: ' + req.session.path_pubBase);

看看是否有效但是有问题

enter image description here

当我使用req.session.NomBase,req.session.CheminHPBase,...在我的路线中它未定义