将列索引转换为字符串时扫描错误

时间:2016-02-03 09:36:17

标签: go

我使用go-sql-driver,但是当我运行代码时。

错误来自: sql:列索引7上的扫描错误:转换字符串" 1.461988e + 06"到int:strconv.ParseInt:解析" 1.461988e + 06":语法无效,

哎呀!问题是什么? Int类型不能分配超过1461988的值?

qs := `SELECT stepdistance,(CASE WHEN stepnumber>=10000 THEN 1 ELSE 0 END),stepnumber,credit1,credit2,credit3,credit4,credit5,credit6,credit7,credit8,stepdaypass,timestamp,walkdate FROM wanbu_stat_activeuser_day_v1_n0 WHERE  activeid=?  AND userid=? AND  walkdate>=? AND walkdate<=? ORDER BY walkdate`
rows, err := db.Query(qs, arg.Aid, uid, start, end)
if err != nil {
    fmt.Println("49", err)
    return err
}
defer rows.Close()
var tmp Userdaytotalstat_s = Userdaytotalstat_s{}
var udts Userdaytotalstat_s = Userdaytotalstat_s{}
var snap Userdaytotalstat_s
var ifarrive = false

for rows.Next() {

    err := rows.Scan(&udts.Stepdistance, &udts.Stepdaywanbu, &udts.Stepnumber,&udts.Credit1, &udts.Credit2, &udts.Credit3, &udts.Credit4, &udts.Credit5, &udts.Credit6,&udts.Credit7, &udts.Credit8, &udts.Stepdaypass, &udts.Timestamp, &udts.Walkdate)

    if err != nil {

        fmt.Println("68", err)
        fmt.Println(qs, arg.Aid, uid, start, end)

        return err
    }

1 个答案:

答案 0 :(得分:1)

无法轻易解析指数表示法,因为字符串表示的值可能会超过64位整数可以容纳的任何值,例如&#34; 2.00 + e1024&#34;。此外,如果仔细观察,字符串表示整数。 清楚地表示浮动的号。

所以,你必须采取一些额外的步骤( Run on Playground

package main

import "fmt"
import "strconv"
import "math/big"

func main() {

    in := "1.461988e+06"
    fmt.Printf("Input:\t\t%v\t(Type: %[1]T)\n",in)

    // The value denoted by in is NOT an integer
    // It is a float with an exponent notation
    // It can not be directly parsed since with exponent notation
    // the integer value represented by in may well exceed anything
    // one of the int types can hold
    if e,err := strconv.ParseFloat("1.461988e+06",10); err != nil {
        fmt.Printf("%v\n",err)
    } else {
        fmt.Printf("Parsed:\t\t%#v\t(Type:%[1]T)\n",e)

        // Hence, we have to use the "big" package
        // And use it's facilities to retrieve the integer
        b := big.NewFloat(e)
        v,p := b.Int64()
        fmt.Printf("Integer:\t%v\t\t(Type:%[1]T,Precision: %v)",v,p)
    }

}