将 mysql 查询行存储在变量中供以后在另一个 mysql 查询中使用
Storing mysql query rows in variable for later use in another mysql query
我有 2 个不同的 table,其中第一个 table 用于检索要存储在第二个 table 中的数据,例如,如果有 3 个项目第一个 table, ill 运行 for 循环 3 次以检索信息并将其逐行存储在第二个 table 中,我还必须为主键使用唯一的 id对于第二个 table,所以我必须 运行 另一个 mysql 查询来检索第二个 table 中的所有 ID 并将 1 添加到 id 以生成新的 ID,但对我来说问题是,当我尝试将 connection.query 放入另一个 connection.query 时,外部 connection.query 运行s 在 运行 内部之前 3 次connection.query,因此项目没有被存储并且 id 没有更新,给我一个重复的主键错误,我尝试使用函数但它没有帮助,这里是
app.get('/submit-check', function (request, response) {
function insert(sql2) {
connection.query(sql2, function (err, result1) {
if (err) throw err;
});
}
function other(value, index) {
let sql = "SELECT * FROM ORDERS"
connection.query(sql, function (err, results) {
var id;
if (results.length == 0) {
id = 0
} else {
id = results[results.length - 1].orderID;
}
// for (let j = 0; j < results.length; j++) {
// list.push(results[j].orderID)
// }
// if (list.length == 0) {
// id = 0
// }
// else {
// var largest = 0;
// for (let i = 0; i <= list.length; i++) {
// if (parseInt(list[i]) > largest) {
// var largest = parseInt(list[i]);
// }
// }
// id = largest + 1
// }
id = id + 1;
var add = request.query.state + " " + request.query.address + " " + request.query.Unumber + " " + request.query.zip
var custID = value[index].custID
var bookID = value[index].bookID
var Email = request.query.email
var CardName = request.query.cardname
var CardNumber = request.query.cardnumber
var ExDate = request.query.expmonth + "/" + request.query.expyear
var CVV = request.query.cvv
var qty = 1
var sql2 = "INSERT INTO orders (orderID,custID, bookID, QTY, CardName, CardNumber, ExDate, CVV, Email, Address, createdAt, updatedAt) VALUES('" + id + "','" + custID + "', '" + bookID + "', '" + qty + "', '" + CardName + "', '" + CardNumber + "', '" + ExDate + "', '" + CVV + "', '" + Email + "', '" + add + "', CURDATE(), CURDATE() )"
insert(sql2)
})
}
function setValue(value) {
for (let index = 0; index < value.length; index++) {
other(value, index)
}
}
let sql3 = "SELECT * FROM carts"
var cart_db;
connection.query(sql3, function (err, result) {
cart_db = result
setValue(cart_db)
})
// console.log(cart_db)
response.render("aftercheck", { attributes: request.query, cardno: "****-****-****" + request.query.cardnumber.slice(-5,) });
})
提前致谢
您的代码有很多很多错误。我在 await/async-based 重写中添加了 TODO 注释,您可能需要在其中修复问题。当然我没能测试这个,因为我没有你的数据库。
async function queryP(connection, sql, parameters = undefined) {
return new Promise((resolve, reject) => {
connection.query(sql, parameters, function (err, results) {
if (err) return reject(err);
resolve(results);
});
});
}
app.get("/submit-check", async function (request, response) {
// TODO: these need to be validated
const {
address,
Unumber,
zip,
expyear,
state,
expmonth,
email: Email,
cardname: CardName,
cardnumber: CardNumber,
cvv: CVV,
} = request.query;
const add = `${state} ${address} ${Unumber} ${zip}`;
const ExDate = `${expmonth}/${expyear}`;
const cartData = await queryP(connection, "SELECT * FROM carts"); // TODO: this is not safe with multiple customers
const [maxOrderIdRow] = await queryP(connection, "SELECT MAX(id) as maxid FROM orders");
const orderId = (maxOrderIdRow.maxid || 0) + 1; // TODO: this is not safe in the face of concurrent requests
// Using a for loop rather than .map and await and so on is simpler here
for (let i = 0; i < cartData.length; i++) {
const cartRow = cartData[i];
const custID = cartRow.custID;
const bookID = cartRow.bookID;
const qty = 1;
// TODO: rethink how credit cards are saved – we can't save them in the database like this or we'll be out of business
await queryP(
connection,
`INSERT INTO orders (orderID, custID, bookID, QTY, CardName, CardNumber, ExDate, CVV, Email, Address, createdAt, updatedAt) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURDATE(), CURDATE() )`,
[orderId, custID, bookID, qty, CardName, CardNumber, ExDate, CVV, Email, add],
);
}
response.render("aftercheck", {
attributes: request.query,
cardno: "****-****-****" + CardNumber.slice(-5),
});
});
我有 2 个不同的 table,其中第一个 table 用于检索要存储在第二个 table 中的数据,例如,如果有 3 个项目第一个 table, ill 运行 for 循环 3 次以检索信息并将其逐行存储在第二个 table 中,我还必须为主键使用唯一的 id对于第二个 table,所以我必须 运行 另一个 mysql 查询来检索第二个 table 中的所有 ID 并将 1 添加到 id 以生成新的 ID,但对我来说问题是,当我尝试将 connection.query 放入另一个 connection.query 时,外部 connection.query 运行s 在 运行 内部之前 3 次connection.query,因此项目没有被存储并且 id 没有更新,给我一个重复的主键错误,我尝试使用函数但它没有帮助,这里是
app.get('/submit-check', function (request, response) {
function insert(sql2) {
connection.query(sql2, function (err, result1) {
if (err) throw err;
});
}
function other(value, index) {
let sql = "SELECT * FROM ORDERS"
connection.query(sql, function (err, results) {
var id;
if (results.length == 0) {
id = 0
} else {
id = results[results.length - 1].orderID;
}
// for (let j = 0; j < results.length; j++) {
// list.push(results[j].orderID)
// }
// if (list.length == 0) {
// id = 0
// }
// else {
// var largest = 0;
// for (let i = 0; i <= list.length; i++) {
// if (parseInt(list[i]) > largest) {
// var largest = parseInt(list[i]);
// }
// }
// id = largest + 1
// }
id = id + 1;
var add = request.query.state + " " + request.query.address + " " + request.query.Unumber + " " + request.query.zip
var custID = value[index].custID
var bookID = value[index].bookID
var Email = request.query.email
var CardName = request.query.cardname
var CardNumber = request.query.cardnumber
var ExDate = request.query.expmonth + "/" + request.query.expyear
var CVV = request.query.cvv
var qty = 1
var sql2 = "INSERT INTO orders (orderID,custID, bookID, QTY, CardName, CardNumber, ExDate, CVV, Email, Address, createdAt, updatedAt) VALUES('" + id + "','" + custID + "', '" + bookID + "', '" + qty + "', '" + CardName + "', '" + CardNumber + "', '" + ExDate + "', '" + CVV + "', '" + Email + "', '" + add + "', CURDATE(), CURDATE() )"
insert(sql2)
})
}
function setValue(value) {
for (let index = 0; index < value.length; index++) {
other(value, index)
}
}
let sql3 = "SELECT * FROM carts"
var cart_db;
connection.query(sql3, function (err, result) {
cart_db = result
setValue(cart_db)
})
// console.log(cart_db)
response.render("aftercheck", { attributes: request.query, cardno: "****-****-****" + request.query.cardnumber.slice(-5,) });
})
提前致谢
您的代码有很多很多错误。我在 await/async-based 重写中添加了 TODO 注释,您可能需要在其中修复问题。当然我没能测试这个,因为我没有你的数据库。
async function queryP(connection, sql, parameters = undefined) {
return new Promise((resolve, reject) => {
connection.query(sql, parameters, function (err, results) {
if (err) return reject(err);
resolve(results);
});
});
}
app.get("/submit-check", async function (request, response) {
// TODO: these need to be validated
const {
address,
Unumber,
zip,
expyear,
state,
expmonth,
email: Email,
cardname: CardName,
cardnumber: CardNumber,
cvv: CVV,
} = request.query;
const add = `${state} ${address} ${Unumber} ${zip}`;
const ExDate = `${expmonth}/${expyear}`;
const cartData = await queryP(connection, "SELECT * FROM carts"); // TODO: this is not safe with multiple customers
const [maxOrderIdRow] = await queryP(connection, "SELECT MAX(id) as maxid FROM orders");
const orderId = (maxOrderIdRow.maxid || 0) + 1; // TODO: this is not safe in the face of concurrent requests
// Using a for loop rather than .map and await and so on is simpler here
for (let i = 0; i < cartData.length; i++) {
const cartRow = cartData[i];
const custID = cartRow.custID;
const bookID = cartRow.bookID;
const qty = 1;
// TODO: rethink how credit cards are saved – we can't save them in the database like this or we'll be out of business
await queryP(
connection,
`INSERT INTO orders (orderID, custID, bookID, QTY, CardName, CardNumber, ExDate, CVV, Email, Address, createdAt, updatedAt) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURDATE(), CURDATE() )`,
[orderId, custID, bookID, qty, CardName, CardNumber, ExDate, CVV, Email, add],
);
}
response.render("aftercheck", {
attributes: request.query,
cardno: "****-****-****" + CardNumber.slice(-5),
});
});