mysql xdevapi nodejs : returning the inserted row auto_increment id

标签 mysql sql node.js

我需要在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/

相关文章:

mysql - 通过sql命令将相同的内容添加到表列

php - 按行顺序加载文件中的数据

javascript - 将我的node.js 模块集成到express.js 应用程序

java - Android 显示来自相互链接表的 PHP 的数据

c++ - 在 while 循环中运行 Qt 程序

Mysql 字符串和数字不相等/不匹配

javascript - socket.io 的额外参数

javascript - 如何在mongodb中replaceRoot后进一步分组对象

mysql - mysql如何处理sql语句中的十六进制

javascript - Nodejs MySQL 静默失败