簡體   English   中英

在似乎有效的查詢上,SQL node.js語法錯誤?

[英]SQL node.js Syntax error on what seems to be a valid query?

我正在對表進行更新以設置位置。 我已經提取了查詢並在數據庫上手動運行它,並且運行良好,但是當通過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代碼,該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
    };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM