我需要在nodejs中的mysql上使用xdevapi执行插入语句后知道主键auto_increment值。
我的代码是:
sqlQueries.getDBConnection(con =>
(function inner(con) {
var query = "INSERT INTO users (name) VALUES ('test')";
var executeSql = con.sql(query).execute();
con.close();
con.done();
return executeSql;
}(con)).then(function (res) {
/***********/
doSomethingWithTheId(id);
/***********/
con.close();
con.done();
}).catch(e => {
cl(e);
})
);
但我不明白如何获取 id 以在 doSomethingWIthTheId() 函数中使用它。
我尝试 console.log(result) 但似乎我得到了一系列方法,但我不知道如何获取我需要的信息。
最佳答案
你能尝试一下吗:
sqlQueries.getDBConnection(con => {
var query = "INSERT INTO users (name) VALUES ('test')";
var executeSql = con.sql(query).execute();
executeSql.then(function(result) {
let id = result.getAutoIncrementValue();
doSomethingWithTheId(id);
}).catch(function(err) {
con.close();
con.done();
})
});
查看更多详细信息:
https://dev.mysql.com/doc/dev/connector-nodejs/8.0/module-Result.html https://dev.mysql.com/doc/dev/connector-nodejs/8.0/module-SqlExecute.html
关于mysql xdevapi nodejs : returning the inserted row auto_increment id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57630592/