我有一个表,其中有一个存储一个非常大的数字的字段(math.big,比 uint64 大)。我将其存储为 DECIMAL 类型:
difficulty NUMERIC NOT NULL,
那么,如何使用library ( ) 从Go
代码中插入此字段?PQ
github.com/lib/pq
此代码不起作用:
me@desk:~/src/github.com/myapp/misc$ cat insertbig.go
package main
import (
"database/sql"
_ "github.com/lib/pq"
"os"
"log"
"math/big"
)
func main() {
var err error
var db *sql.DB
std_out:=log.New(os.Stdout,"",0)
conn_str:="user='testuser' dbname='testdb' password='testpasswd'";
db,err=sql.Open("postgres",conn_str);
if (err!=nil) {
log.Fatal(err);
}
_,err=db.Exec("CREATE TABLE bigtable(difficulty NUMERIC)");
difficulty:=big.NewInt(0);
difficulty.SetString("1111111111111111111111111111111111111111111111111111111111111111111111",10);
_,err=db.Exec("INSERT INTO bigtable(difficulty) VALUES(?)",difficulty);
if (err!=nil) {
log.Fatal(err);
} else {
std_out.Println("record was inserted");
}
}
me@desk:~/src/github.com/myapp/misc$
它给了我这个错误:
2018/02/05 17:00:25 sql: converting argument $1 type: unsupported type big.Int, a struct