Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript SQL node.js语法错误,似乎是一个有效的查询?_Javascript_Sql_Node.js_Syntax Error - Fatal编程技术网

Javascript SQL node.js语法错误,似乎是一个有效的查询?

Javascript SQL node.js语法错误,似乎是一个有效的查询?,javascript,sql,node.js,syntax-error,Javascript,Sql,Node.js,Syntax Error,我正在对一个表进行更新以设置位置。我提取了查询并在数据库上手动运行它,工作正常,但当通过connection.query()传递时,它似乎认为node.js控制台中存在语法错误 function sendShipPosition(position) { var input = ''; if (position.moving === true) { var currentdate = new Date(); var datetime = cur

我正在对一个表进行更新以设置位置。我提取了查询并在数据库上手动运行它,工作正常,但当通过connection.query()传递时,它似乎认为node.js控制台中存在语法错误

function sendShipPosition(position) {

    var input = '';

    if (position.moving === true) {
        var currentdate = new Date(); 
        var datetime = currentdate.getFullYear() + "-"  
                    + (currentdate.getMonth()+1)  + "-" 
                    + currentdate.getDate() + " "
                    + currentdate.getHours() + ":"  
                    + currentdate.getMinutes() + ":" 
                    + currentdate.getSeconds();
        var input = ', moving_datetime = ' + datetime;
    }

    connection.query('UPDATE ships SET x_axis = :x, y_axis = :y' + input + ' WHERE ship_id = :ship_id'), {
        x: parseInt(position.x),
        y: parseInt(position.y),
        ship_id: 1
    };
}
以下是语法错误:

以下是“位置”变量的输入数据值:

{ x: '-605', y: '-257', moving: 0 }
我希望我不是一个太笨的人,对这个低质量的问题感到抱歉


谢谢

此函数将生成SQL代码,该代码在
datetime
变量周围缺少引号,从而导致SQL代码无效

function sendShipPosition(position) {

    var input = '';

    if (position.moving === true) {
        var currentdate = new Date(); 
        var datetime = currentdate.getFullYear() + "-"  
                    + (currentdate.getMonth()+1)  + "-" 
                    + currentdate.getDate() + " "
                    + currentdate.getHours() + ":"  
                    + currentdate.getMinutes() + ":" 
                    + currentdate.getSeconds();
        # Here!
        var input = ', moving_datetime = \'' + datetime + '\''
    }

    connection.query('UPDATE ships SET x_axis = :x, y_axis = :y' + input + ' WHERE ship_id = :ship_id'), {
        x: parseInt(position.x),
        y: parseInt(position.y),
        ship_id: 1
    };
}

我再次收到一个错误,然后在我的所有值周围加上引号。在输入周围加上引号就像SQL101:回到基础!啊!感谢您的帮助:)您正在为X和Y使用占位符,这很好。为什么不为日期值使用占位符呢?我觉得它更干净,因为我还必须将值放入对象中,才能在其中获取日期时间。另外,我假设使用“new Date();”的输入没有问题?然而现在我想起来了,你说得很好,我很懒,不应该违背最佳实践。我会更新我的代码,谢谢。