0

我正在尝试使用 NodeJS 和 node-mysql 根据时间戳选择一系列记录。我的代码如下所示:

sql = "SELECT pressure, temp1, temp2, UNIX_TIMESTAMP(timestamp)*1000 
AS unix_timestamp
FROM observations WHERE timestamp >= STR_TO_DATE( ? ,'%Y-%m-%d') 
ORDER BY unix_timestamp DESC LIMIT ?;"

var current_temp = db.query(sql,
start_date, num_records,
  function(err, rows, fields){
     if (err){

变量 start_date 包含 2013-03-01。如果我手动替换“?” 在带有 '2013-03-01' 的 STR_TO_DATE 表达式中,我的查询有效。但是,如果我尝试进行变量替换,我会收到以下错误:

TypeError: Object 500 has no method 'apply'
at Query.Sequence.end     (/home/ian/Documents/emacs/org/org_files/computer/nodejs/node_modules/mysql/lib/protocol/sequences/Sequence.js:66:24)
at Query.ErrorPacket     (/home/ian/Documents/emacs/org/org_files/computer/nodejs/node_modules/mysql/lib/protocol/sequences/Query.js:89:8)
at Protocol._parsePacket     (/home/ian/Documents/emacs/org/org_files/computer/nodejs/node_modules/mysql/lib/protocol/Protocol.js:169:24)
at Parser.write     (/home/ian/Documents/emacs/org/org_files/computer/nodejs/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write     (/home/ian/Documents/emacs/org/org_files/computer/nodejs/node_modules/mysql/lib/protocol/Protocol.js:36:16)
at write (_stream_readable.js:547:24)
at flow (_stream_readable.js:556:7)
at Socket.pipeOnReadable (_stream_readable.js:588:5)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:382:10)
4

1 回答 1

0

您的变量应该在一个数组中,根据它们在 SQL 语句中出现的顺序定位,如下所示:

    db.query(sql,[start_date, num_records],  function(err, rows, fields){ logic ;})
于 2013-03-19T13:06:34.417 回答