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

即使插入成功,为什么在插入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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵