dqyknf4423
2018-12-31 19:23
浏览 228

如何从sqlx获取最后插入的行的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)

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

resultPost is: {0xc420242000 0xc4202403a0}

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

图片转代码服务由CSDN问答提供 功能建议

我想找回使用 sqlx插入MySql数据库的最新帖子的ID

  resultPost,错误:= shared.Dbmap.Exec(“ INSERT INTO post(user_id,description,link)VALUES(?,?,?)”,userID  ,标题,destPath)
if err!= nil {
 log.Println(err)
 c.JSON(
 http.StatusInternalServerError,
 gin.H {“ error”:“内部服务器错误”}))\  n} 
 
fmt.Println(“ resultPost is:”,resultPost)
   
 
 

问题是 resultPost 打印为 对象:

  resultPost是:{0xc420242000 0xc4202403a0} 
   
 
 

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

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dslpofp041310584 2018-12-31 19:27
    最佳回答

    Looks like you just need :

    resultPost.LastInsertId()
    

    For more information, search for LastInsertId in this documentation

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题