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 關注
- 228 瀏覽
添加回答
舉報