如何从sqlx获取最后插入的行的ID? [英] How to get id of last inserted row from sqlx?

查看:43
本文介绍了如何从sqlx获取最后插入的行的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想取回使用 sqlx 插入到 MySql 数据库中的最后一篇文章的 ID:

I'd like to get back the id of the last post inserted into MySql database using sqlx:

resultPost, err := shared.Dbmap.Exec("INSERT INTO post (user_id, description, link) VALUES (?, ?, ?)", userID, title, destPath)
if err != nil {
    log.Println(err)
    c.JSON(
        http.StatusInternalServerError,
        gin.H{"error": "internal server error"})
}

fmt.Println("resultPost is:", resultPost)

问题在于 resultPost 被打印为一个对象:

The problem is that the resultPost is printed as an object:

resultPost is: {0xc420242000 0xc4202403a0}

所以我想知道提取刚刚插入的行的 id 的正确方法是什么?

So I'm wondering what is the correct way to extract the id of the row just inserted?

推荐答案

看起来你只需要:

resultPost.LastInsertId()

有关详细信息,请在 本文档

这篇关于如何从sqlx获取最后插入的行的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆