我想將我的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