0

我正在尝试从 MYSQL 中的表中提取数据并将其保存到另一个表中。这是我的代码:这两个表具有完全相同的列。注意:该行确实被保存,但每个值显然为零。每当我保存它时,MYSQL 都会创建一个带有索引的新行,但所有值都为零。

client.query('SELECT * FROM archive ORDER BY stamp LIMIT 1', function(err, result){
        var nor = result[0].Northatt;
        var eas = result[0].Eastatt;
        var sou = result[0].Southatt;
        var wes = result[0]. Westatt;
        var time = result[0].stamp;

        client.query("INSERT INTO quartly (Northatt, Eastatt, Southatt, Westatt, stamp) VALUES ('+nor+','+eas+','+sou+','+wes+','+time+')", function()err{
});
});

所有变量都持有它们各自的'int'......很确定我也在使用正确的语法来保存变量。

任何帮助,将不胜感激!

4

2 回答 2

0

转义参数客户端:

client.query('SELECT * FROM archive ORDER BY stamp LIMIT 1', function(err, result){
        var nor = result[0].Northatt;
        var eas = result[0].Eastatt;
        var sou = result[0].Southatt;
        var wes = result[0].Westatt;
        var time = result[0].stamp;

        client.query("INSERT INTO quartly (Northatt, Eastatt, Southatt, Westatt, stamp) VALUES (?,?,?,?,?)", [nor, eas, sou, wes, time], function()err{
        });
});

或者使用带有node-mysql2 的准备好的语句(这样参数是独立发送的,而不是作为 sql 查询字符串的一部分)

client.query('SELECT * FROM archive ORDER BY stamp LIMIT 1', function(err, result){
        var nor = result[0].Northatt;
        var eas = result[0].Eastatt;
        var sou = result[0].Southatt;
        var wes = result[0].Westatt;
        var time = result[0].stamp;

        client.execute("INSERT INTO quartly (Northatt, Eastatt, Southatt, Westatt, stamp) VALUES (?,?,?,?,?)", [nor, eas, sou, wes, time], function()err{
        });
});

您也可以在一个查询中执行此操作 - 请参阅'SQL Insert into ... values (SELECT ... FROM ...)' SO question

于 2013-07-30T00:40:46.220 回答
0

你错过了这样的双引号

    VALUES ('" +nor+"','"+eas+"','"+sou+"','"+wes+"','"+time+"')
于 2013-07-29T17:30:31.747 回答