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 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符