我正在尝试使用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)
答案 0 :(得分:0)
您的变量应该在一个数组中,根据它们在SQL语句中出现的顺序定位,如下所示:
db.query(sql,[start_date, num_records], function(err, rows, fields){ logic ;})