结构体 Time 的属性在 Go 的 sqlx 库中无法加载。

huangapple go评论70阅读模式
英文:

struct Time property doesn't load from Go sqlx library

问题

我有一个带有时间属性的结构体:

type Basket struct {  
  ...  
  Created_at time.Time `db:"created_at"`
}

时间保存为:

basket.Created_at = time.Now().UTC()

如果我使用Insert SQL语句保存它,在SQLite3中时间会被正确保存,但是当我使用以下语句选择所需的记录时:

ret_basket := Basket{}
err := database.DB.Get(&ret_basket, "SELECT id, ..., created_at FROM baskets WHERE user_id = ?", some_user_id)

它返回的记录中,其他属性都加载正确,但时间属性ret_basket.Created_at的值为0001-01-01 00:00:00 +0000 UTC

有什么建议吗?

英文:

I have a struct with a property time:

type Basket struct {  
  ...  
  Created_at time.Time `db:"created_at"`
}

with the time saved as:

basket.Created_at = time.Now().UTC()

If I save it using Insert sql statement, it saves the time nicely in the SQLite3 but when I select the desired record using:

ret_basket := Basket{}
err := database.DB.Get(&ret_basket, "SELECT id, ..., created_at FROM baskets WHERE user_id = ?", some_user_id)

It returns the record with other properties loaded properly except the time property which is ret_basket.Created_at as 0001-01-01 00:00:00 +0000 UTC

Any suggestions?

答案1

得分: 6

没有官方的Sqlite包,所以我假设你正在使用https://github.com/mattn/go-sqlite3。可能你的问题是由于数据库中的created_at字段声明错误导致的,应该是DATETIME类型。因为下面的代码在我的机器上运行得很好(我已经删除了所有的错误检查):

package main

import (
	"github.com/jmoiron/sqlx"
	_ "github.com/mattn/go-sqlite3"
	"log"
	"time"
)

type Post struct {
	Id      int64     `db:"post_id"`
	Created time.Time `db:"created"`
}

func main() {
	db, _ := sqlx.Connect("sqlite3", "post_db.db")
	db.MustExec("DROP TABLE IF EXISTS posts; CREATE TABLE posts (post_id INT, created DATETIME);")

	p1 := Post{Id: 1, Created: time.Now().UTC()}
	p2 := Post{}

	tx := db.MustBegin()
	tx.NamedExec("INSERT INTO posts (post_id, created) VALUES (:post_id, :created)", &p1)
	tx.Commit()

	db.Get(&p2, "SELECT post_id, created FROM posts WHERE post_id = $1", p1.Id)
	log.Println(p2.Created.Format("2006-01-02"))
}
英文:

There is no official package for Sqlite so I assume you are using https://github.com/mattn/go-sqlite3 Probably your issue is result of wrong created_at field declaration in database which should be DATETIME because the next code works perfectly on my machine (I've removed all error checks):

package main

import (
	"github.com/jmoiron/sqlx"
	_ "github.com/mattn/go-sqlite3"
	"log"
	"time"
)

type Post struct {
	Id      int64     `db:"post_id"`
	Created time.Time `db:"created"`
}

func main() {
	db, _ := sqlx.Connect("sqlite3", "post_db.db")
	db.MustExec("DROP TABLE IF EXISTS posts; CREATE TABLE posts (post_id INT, created DATETIME);")

	p1 := Post{Id: 1, Created: time.Now().UTC()}
	p2 := Post{}

	tx := db.MustBegin()
	tx.NamedExec("INSERT INTO posts (post_id, created) VALUES (:post_id, :created)", &p1)
	tx.Commit()

	db.Get(&p2, "SELECT post_id, created FROM posts WHERE post_id = $1", p1.Id)
	log.Println(p2.Created.Format("2006-01-02"))
}

huangapple
  • 本文由 发表于 2016年1月30日 01:25:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/35090436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定