该问题仅针对github.com/jmoiron/sqlx
。
我想用命名查询插入一个已知的时间戳或日期。我怎么做?我使用time.Now()
作为给定时间戳的示例。
这是我在MySQL中的表:
CREATE TABLE book (
id int(11) NOT NULL,
name varchar(50) DEFAULT NULL,
ts timestamp NULL DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB
package main
import (
_ "database/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"log"
"time"
"fmt"
)
type book struct {
Id int
Name string
Ts time.Time
}
func main() {
db, err := sqlx.Open("mysql", "xxx:xxxxx@tcp(127.0.0.1:3306)/dbname?parseTime=true&charset=utf8")
if err != nil {
log.Println(err)
}
defer db.Close()
v := book{Id: 1, Name: "Book", Ts: time.Now() }
fmt.Println(v)
stmt, err := db.PrepareNamed(`INSERT INTO book VALUES (:id, :name, :ts)`)
_, err = stmt.Exec(v)
}
这是我的错误:
{1 Book 2019-08-06 09:21:15.178503955 +0000 UTC m=+0.000601133}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x604ec6]
goroutine 1 [running]:
github.com/jmoiron/sqlx.(*NamedStmt).Exec(0x0, 0x65c980, 0xc00010a060, 0xc00010a060, 0x0, 0x6cfaa0, 0xc000112020)
/root/go/src/github.com/jmoiron/sqlx/named.go:43 +0x26
main.main()
/root/go/src/dbtest/sqlx_insert.go:37 +0x284
exit status 2
答案 0 :(得分:0)
对不起!时间戳列名称实际上是由自动化脚本更改的。虚惊一场!一切正常。感谢大家!