dongwu3596 2018-10-26 00:39
浏览 52

从数据库/ SQL查询获取结果

I have this:

tx, _ := db.Begin()
p := person.Person{Handle: "foo"}

rows, err := tx.Exec("INSERT INTO person (handle, email) VALUES ($1, $2) RETURNING id", p.Handle, p.Email).         

notice how it says RETURNING id in the sql query..how can I get the id from the first row return by the query?

I tried this:

var id string
defer rows.Close()
for rows.Next() {
    err := rows.Scan(&id, &name)
    if err != nil {
        log.Fatal(err)
    }
    log.Println(id, name)
}

but I get this compile error:

rows.Close undefined (type sql.Result has no field or method Close)

  • 写回答

1条回答

  • duanci3845 2018-10-26 02:21
    关注

    There is no Next() on rows which get from Exec(). But it has LastInsertId() method which you can get the id.

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办