Kar*_*lom 3 mysql sql go sql-insert sqlx
我想使用以下方法取回插入 MySql 数据库的最后一篇文章的 id 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打印为对象:
resultPost is: {0xc420242000 0xc4202403a0}
所以我想知道提取刚刚插入的行的 id 的正确方法是什么?
Exec,的返回值Result并不意味着可以直接访问——它是一个有两个方法可以调用的对象,其中之一是LastInsertId().
lastId, err := resultPost.LastInsertId()
if err != nil {
    panic(err)
}
fmt.Println("LastInsertId: ", lastId)
| 归档时间: | 
 | 
| 查看次数: | 5675 次 | 
| 最近记录: |