无法将0设置为整数

时间:2018-11-01 19:07:02

标签: go sqlite

我有更新数据库的简单功能。我使用SQLite3,所以将INTEGER字段用作bool

这是这里的功能:

func updateDevice(devID int64, videoPath string, active bool) {
        stmt, err := db.Prepare("UPDATE Devices SET CurrentVideo=?, Active=? WHERE ID=?")
        if err != nil {
                log.Fatalf("Error while preparing to update device with ID: %s: %s", devID, err)
        }

        res, err := stmt.Exec(videoPath, devID, active)
        if err != nil {
                log.Fatalf("Error while updating device with ID: %s : %s", devID, err)
        }
        rowsAff, _ := res.RowsAffected()
        if rowsAff > 0 {
                log.Printf("Successfully update device with ID: %v", devID)
        } else {
                log.Println("Didn't affect on any row")
        }
}

当我将active设置为true时,会收到一条消息,提示我已成功更新设备,但是当我要将active设置为false时,则会收到此消息: Didn't affect on any row,在数据库中,我仍然有旧值。

为什么当我将false用作active时无法更新此表?我在做什么错了?

1 个答案:

答案 0 :(得分:2)

在执行程序中,您必须按照准备值的顺序传递值。

您在准备中有以下顺序:

CurrentVideo=?, Active=? WHERE ID=?

在我看来,您在exec中已经颠倒了“ active”和“ devID”值。

func updateDevice(devID int64, videoPath string, active bool) {
        stmt, err := db.Prepare("UPDATE Devices SET CurrentVideo=?, Active=? WHERE ID=?")
        if err != nil {
                log.Fatalf("Error while preparing to update device with ID: %s: %s", devID, err)
        }

        res, err := stmt.Exec(videoPath, active, devID)
        if err != nil {
                log.Fatalf("Error while updating device with ID: %s : %s", devID, err)
        }
        rowsAff, _ := res.RowsAffected()
        if rowsAff > 0 {
                log.Printf("Successfully update device with ID: %v", devID)
        } else {
                log.Println("Didn't affect on any row")
        }
}

尝试这种方式。