英文:
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>=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
得分: 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 "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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论