dongyudun6465 2018-11-27 20:39
浏览 329
已采纳

即使插入成功,为什么在插入postgres之后仍得到ErrNoRows(“ sql:结果集中无行”)?

I am working with golang-postgres:

"database/sql"
_ "github.com/lib/pq"

What I am doing:

I run a select query. If I don't get any entry on selecting, I go ahead and insert one. Else, update, or something else...

The problem is, every time insert returns ErrNoRows("sql: no rows in result set") , even when I can see in database that INSERT was successful, and row was added.

Also lastInsertID always remains 0, while if I check in DB, it has an actual value(say, 131)

It seems as if the error near comment 2 below in the code, is cached somewhere, and shows up again.

// 1. Select first
sQueryStmt, sPrepErr := db.Prepare(selectQuery)
    if sPrepErr != nil {
        glog.V(3).Infof("selectQuery prepare failed. err: %v", sPrepErr)
        return sPrepErr
    }

    queryErr := sQueryStmt.QueryRow(today, itemID).Scan(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j)
    if queryErr != nil {

// 2. Getting ErrNoRows here is expected, for the first entry
        if queryErr == sql.ErrNoRows {
            glog.V(3).Infof("selectQuery returned no results. err: %v", queryErr)

            var lastInsertID int
            iQueryStmt, iPrepErr := db.Prepare(insertQuery)
            if iPrepErr != nil {
                glog.V(3).Infof("insertQuery prepare failed. err: %v", iPrepErr)
                return iPrepErr
            }
            insertErr := iQueryStmt.QueryRow(today, today, today, taskID).Scan(&lastInsertID)
            if insertErr != nil {
                // 3. Somehow, even after row is correctly being created, I am getting insertErr as "sql: no rows in result set" here
                glog.V(3).Infof("insertQuery failed. err: %v", insertErr)
            } else {
// control never comes to this part.
                glog.V(3).Infof("insertQuery Successful. lastInsertID: %v", lastInsertID)
            }
        } else {
            glog.V(3).Infof("Couldn't run query on Postgres database. err: %v", queryErr)
            return queryErr
        }
}

Debugging inside database/sql, shows that it is returning this error in below code block:

    if !r.rows.Next() {
        if err := r.rows.Err(); err != nil {
            return err
        }
        return ErrNoRows
    }

But every time, if I check in DB, the data is always already inserted.

Edit:

selectQuery = `select task_id, creation_date, times_x_ran,
                        a_count, b_count, c_count, d_count, e_count 
                        from reports where creation_date = $1 and task_id = $2`

insertQuery = `insert into reports 
                (creation_date, creation_time, last_modified, task_id, times_x_ran, times_y_ran, a, b, c, d, e, x_errors, y_errors) 
                    values 
                    ($1, $2, $3, $4,
                    1, 0, 
                    0, 0, 0, 0, 0, 
                    '', '' )`
  • 写回答

1条回答 默认 最新

  • dongni1892 2018-11-28 07:10
    关注

    You've effectively asked two questions, so I'll answer both:

    1. Why ErrNoRows even when your insert is successful?

      This is expected behavior, because by default an INSERT returns no rows. Your "error" isn't actually problematic. If you want an INSERT to return rows, you must use a 'RETURNING' clause at the end.

      The fact that the "No rows in result set" state is communicated by way of an error may be the reason for your confusion. But this should only be considered an error if you expected rows.

    2. Also lastInsertID always remains 0.

      This is also expected behavior. As documented:

      pq does not support the LastInsertId() method of the Result type in database/sql. To return the identifier of an INSERT (or UPDATE or DELETE), use the Postgres RETURNING clause with a standard Query or QueryRow call:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料