在将字符串转换为整数时,列索引出现扫描错误。

huangapple go评论94阅读模式
英文:

Scan error on column index converting string to a int

问题

我使用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
    }
}

以上是代码部分。

英文:

I use go-sql-driver, but when I run the code .

Errors comes with:
sql: Scan error on column index 7: converting string "1.461988e+06" to a int: strconv.ParseInt: parsing "1.461988e+06": invalid syntax ,

Ooops ! what's the problem ? Int type can not assigned a value more than 1461988 ?

qs := `SELECT stepdistance,(CASE WHEN stepnumber&gt;=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&gt;=? AND walkdate&lt;=? ORDER BY walkdate`
rows, err := db.Query(qs, arg.Aid, uid, start, end)
if err != nil {
	fmt.Println(&quot;49&quot;, 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(&amp;udts.Stepdistance, &amp;udts.Stepdaywanbu, &amp;udts.Stepnumber,&amp;udts.Credit1, &amp;udts.Credit2, &amp;udts.Credit3, &amp;udts.Credit4, &amp;udts.Credit5, &amp;udts.Credit6,&amp;udts.Credit7, &amp;udts.Credit8, &amp;udts.Stepdaypass, &amp;udts.Timestamp, &amp;udts.Walkdate)

	if err != nil {

		fmt.Println(&quot;68&quot;, err)
		fmt.Println(qs, arg.Aid, uid, start, end)

		return err
	}

答案1

得分: 1

指数表示法不容易解析,因为字符串表示的值可能超过64位整数可以容纳的范围,例如"2.00+e1024"。此外,如果你仔细观察,这个字符串并不表示一个整数。小数点明显表示一个浮点数。

因此,你需要采取一些额外的步骤(在 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)

    // in 表示的值不是整数
    // 它是一个带有指数表示法的浮点数
    // 由于指数表示法,in 表示的整数值可能超过任何 int 类型可以容纳的范围
    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)

        // 因此,我们必须使用 "big" 包
        // 并使用它的功能来获取整数
        b := big.NewFloat(e)
        v,p := b.Int64()
        fmt.Printf("Integer:\t%v\t\t(Type:%[1]T,Precision: %v)",v,p)
    }

}

以上是给定代码的翻译结果。

英文:

Exponent notations can not be parsed easily, since the value denoted by the string may well exceed anything a 64bit integer can hold, e.g "2.00+e1024". Furthermore, if you look closely, the string does not represent an integer. The point clearly denotes a floating point number.

So, you have to take some additional steps (<kbd>Run on Playground</kbd>)

package main

import &quot;fmt&quot;
import &quot;strconv&quot;
import &quot;math/big&quot;

func main() {

	in := &quot;1.461988e+06&quot;
	fmt.Printf(&quot;Input:\t\t%v\t(Type: %[1]T)\n&quot;,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(&quot;1.461988e+06&quot;,10); err != nil {
		fmt.Printf(&quot;%v\n&quot;,err)
	} else {
		fmt.Printf(&quot;Parsed:\t\t%#v\t(Type:%[1]T)\n&quot;,e)
	
		// Hence, we have to use the &quot;big&quot; package
		// And use it&#39;s facilities to retrieve the integer
		b := big.NewFloat(e)
		v,p := b.Int64()
		fmt.Printf(&quot;Integer:\t%v\t\t(Type:%[1]T,Precision: %v)&quot;,v,p)
	}

}

huangapple
  • 本文由 发表于 2016年2月3日 17:36:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/35173233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定