SQL update
SQLupdate.js
app.get("/update", function (req, res, next) {
var count = 0;
pool.update("update member set prof=? WHERE id=?", ["I tried to update", 100])
.then(function (nUpdated) {
count = nUpdated;
res.send(count + "Updated");
});
});
The value returned by the callback is set to the updated number. Update result
SQL insert
SQLinsert.js
app.get("/insert", function (req, res, next) {
var insertIds = "";
var tableName = "MEMBER", idColumn = "ID", rows = [
{ ID: 105, LNAME: "Oda", FNAME: "Nobunaga", PROF: "Before reaching the goal ...", TOKUTEN: 10.5 },
{ ID: 106, LNAME: "Toyotomi", FNAME: "Hideyoshi", PROF: "We unified the world.", TOKUTEN: 10.6 }
];
pool.insertList(tableName, idColumn, rows)
.then(function (listOfGeneratedIds) {
for (var _i = 0, listOfGeneratedIds_1 = listOfGeneratedIds; _i < listOfGeneratedIds_1.length; _i++) {
var insertId = listOfGeneratedIds_1[_i];
insertIds = insertIds + insertId.toString() + ",";
}
res.send(insertIds + "Was inserted");
});
});
Unfortunately this could not be done on V5R4. Although it is the cause, it seems that the following SQL is issued internally.
SELECT ID FROM NEW TABLE(INSERT INTO MEMBER (ID, LNAME, FNAME, PROF, TOKUTEN) VALUES(?, ?, ?, ?, ?), (?, ?, ?, ?, ?))
params: [105,"Oda","Nobunaga","Before reaching the goal ...",10.5,106,"Toyotomi","Hideyoshi","Unified the world",10.6]
It seems that this SQL cannot be interpreted, and an error occurs.
I was able to execute it on V7R2, so I will post the result.
SQL batch update
SQLbatchUpdate.js
app.get("/batchUpdate", function (req, res, next) {
var insertIds = "";
var data = [
[107, "Tokugawa","Ieyasu","The first generation",1],
[108, "Tokugawa","Hidetada","Second generation",2],
[109, "Tokugawa","Iemitsu","Third generation",3]
];
pool.batchUpdate("INSERT INTO MEMBER (ID,LNAME,FNAME,PROF,TOKUTEN) VALUES(?,?,?,?,?)", data)
.then(function (result) {
for (var _i = 0, result_1 = result; _i < result_1.length; _i++) {
var insertId = result_1[_i];
insertIds = insertIds + insertId.toString() + ","; //result is number of updated rows for each row. [1, 1, 1] in this case.
}
res.send(insertIds + "Was inserted");
});
});
This could be done on V5R4. The return value seems to return the number of updates for each row.
Recommended Posts