我是新来的。我使用github.com/go-sql-driver/mysql作为我的驱动程序我有一个表,其中有许多列的数据类型是 decimal(65,0) . 我正在尝试使用big.int在我的应用程序中处理这些对我来说似乎合理的值。但是,我找不到使用 Stmt.Exec . 上面写着sql:转换参数$2类型:不支持的类型big.int,结构另外,我也找不到方法将它从mysql序列化回我的go结构。
decimal(65,0)
Stmt.Exec
6tr1vspr1#
---------+输出:
n: 12345678901234567891234567 n: -12345678901234567891234567 n: <nil> n: -12345678901234567891234567
bqucvtff2#
对sql语句使用字符串值。转换为和从 *big.Int 价值观。例如,
*big.Int
package main import ( "fmt" "log" "math/big" "database/sql" _ "github.com/go-sql-driver/mysql" ) func printN(rows *sql.Rows) { for rows.Next() { var n *big.Int var s sql.NullString err := rows.Scan(&s) if err != nil { log.Print(err) } n, _ = new(big.Int).SetString(s.String, 10) fmt.Printf("n: %v\n", n.String()) } err := rows.Err() if err != nil { log.Print(err) } } func main() { db, err := sql.Open("mysql", "peter:@/so") if err != nil { log.Fatal(err) } defer db.Close() query := `SELECT n from so.dec65;` rows, err := db.Query(query) if err != nil { log.Fatal(err) } printN(rows) query = `SELECT n from so.dec65 WHERE n < ?;` arg1, ok := new(big.Int).SetString("12345678901234567891234567", 10) if ok { rows, err = db.Query(query, arg1.String()) if err != nil { log.Fatal(err) } printN(rows) } }
数据库:
mysql> DESCRIBE dec65; +-------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------+------+-----+---------+-------+ | n | decimal(65,0) | YES | | NULL | | +-------+---------------+------+-----+---------+-------+ mysql> SELECT n FROM dec65; +-----------------------------+ | n | +-----------------------------+ | 12345678901234567891234567 | | -12345678901234567891234567 | | NULL |
2条答案
按热度按时间6tr1vspr1#
---------+
输出:
bqucvtff2#
对sql语句使用字符串值。转换为和从
*big.Int
价值观。例如,数据库: