我想将我的mysql查询中的检索值存储到一个变量中,以便我可以将该值插入到另一个表中。如何将mysql值存储到变量中?
下面是检索值
connectionSelectPackageId.connect();
let sqlSelectPackageId = "SELECT MAX(packageId) as maxId FROM recipients";
connectionSelectPackageId.query(sqlSelectPackageId, function(err, rows){
if(err) {
throw err;
} else {
for(var i=0; i< rows.length; i++) {
console.log(rows[i].maxId);
};
};
});
connectionSelectPackageId.end();
我知道我是正确的检索值,因为当我安慰出来它打印出正确的数量
console.log(rows[i].maxId);
我想查询存储值工作通过连接结束(connectionSelectPackageId.end();
)
在此先感谢
connectionInsert.connect();
// Insert into the recipients the hashtag and information needed to opon the lock
function getDateTime() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth()+1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
if(month.toString().length == 1) {
var month = '0'+month;
}
if(day.toString().length == 1) {
var day = '0'+day;
}
if(hour.toString().length == 1) {
var hour = '0'+hour;
}
if(minute.toString().length == 1) {
var minute = '0'+minute;
}
if(second.toString().length == 1) {
var second = '0'+second;
}
var dateTime = year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second;
return dateTime;
}
var dateTime = getDateTime();
// Add email later on
let sqlInsert = 'INSERT INTO recipients (date, ..., ..., ..., ..., ..., ..., data) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
connectionInsert.query(sqlInsert, [dateTime, ..., ..., ...,...,..., ..., data], function(err, rows, fields) {
});
connectionInsert.end();
的数据是在第一查询或rows[i].maxId
为什么不把它放在一个变量中,它的范围足以满足您的需求(即全局范围)?只是'var data = rows [i] .maxId' – RLaaa
我试过了,但它似乎只能在查询范围内工作。 – John
我用这个var data = rows [i] .maxId替换控制台日志,但是当我做控制台日志数据超出查询范围时它说这个变量是未定义的 – John