I have a table where I have a field which stores a really big number (math.big, which is bigger than uint64). I am storing it in a DECIMAL type:
difficulty NUMERIC NOT NULL,
So, how do I insert this field from Go
code using PQ
library (github.com/lib/pq
) ?
This code does not work:
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$
It gives me this error:
2018/02/05 17:00:25 sql: converting argument $1 type: unsupported type big.Int, a struct