duanhan4763 2013-09-24 10:06
浏览 84
已采纳

使用Go和数据库/ SQL扫描nil float值时出错

I'm writing a program that needs to determine opening values for a table prior to doing some Inserts and Updates for that table. The table in question (PostgreSql in this case) could have zero rows initially. When I select the opening values, if there are zero rows, the total for the value of balances is being returned as nil. This causes the scan to fail with message :

Error on scan of test01 opening Row Count. Error = sql: Scan error on column
index 1: converting string "<nil>" to a float64: strconv.ParseFloat:
parsing "<nil>": invalid syntax

While I can "solve" the problem by doing two selects, one to select the COUNT(*) and the other to SUM() the balances if the row-count exceeds zero, it does not seem an elegant solution, and may not always solve the problem, and the two values selected (number of rows and total of balances) are not at the same point in time.

Is there a way to solve this problem doing one select of the table?

A small test program illustrating the problem is below. When there are rows in the table being selected, the program works fine. However if there are zero rows, the above error results.

Example Test Program:

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/lib/pq"
)

var db *sql.DB

func main() {
    var err error

    db, err = sql.Open("postgres",
              "user=test dbname=testdb password=test sslmode=disable")
    if err != nil {
        fmt.Sprintf("Failed to open Db Connection. Error = %s
", err)
        return
    }

    defer fCloseDb()

    var row *sql.Row
    var sSql string = "SELECT COUNT(*), SUM(dbalance) FROM test01"
    if row = db.QueryRow(sSql); row == nil {
        println("No row returned selecting opening count(*) from test01")
        return
    }

    var iRowCount int64 = 0
    var fBalTot float64 = 0.00

    if err = row.Scan(&iRowCount, &fBalTot); err != nil {
        fmt.Printf("Error on scan of test01 opening Row Count. Error = %s
", err)
        return
    }

    fmt.Printf("Row Count = %d, Balance total value = %.2f
", iRowCount, fBalTot)
}

func fCloseDb() {
    if db != nil {
        db.Close()
        println("Db Closed")
    }
}

The structure of the table is as follows :

sSql = "CREATE TABLE IF NOT EXISTS test01 " +
       "(ikey SERIAL Primary Key, " +
       "sname varchar(22) not null, " +
       "dbalance decimal(12,2) not null)"
  • 写回答

1条回答 默认 最新

  • duandou8120 2013-09-24 15:28
    关注

    Many thanks, that worked :

    "SELECT COUNT(*), coalesce(SUM(dbalance), 0.00) FROM test01"
    

    I believe that coalesce returns the first non-null value.

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

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真