1 回答
TA贡献1797条经验 获得超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"))
}
- 1 回答
- 0 关注
- 189 浏览
添加回答
举报