duancuisan2503 2017-12-11 17:38
浏览 933

如何在Postgresql时间戳中存储Golang time.time?

May I know how to store the time.time object in Postgresql?

For example, the SQL query:

INSERT INTO "UserAccount" ("email", "login_time") VALUES ('human@example.com', 2017-12-12 00:58:26.9589451 +0800 +08 m=+1406.914186601)

I tried to use loginTime := time.Now(), and it gives a time format that Postgresql don't really understand, e.g. 2017-12-12 00:58:26.9589451 +0800 +08 m=+1406.914186601

But if I try to use loginTime := time.Now().Format(time.RFC3339), and the compiler complain that loginTime is a string, and I need it to be time.time type.

May I know how to handle this?

Sorry for asking newbie question, new to both Golang and Postgres. :)

  • 写回答

2条回答 默认 最新

  • dongpao1926 2017-12-11 18:08
    关注

    The pq driver (which I assume you are using) automatically converts time.Time instances correctly for you.

    so, you can do:

    db.Exec(`INSERT INTO "UserAccount" ("email", "login_time") VALUES ($1, $2)`,"human@example.com",time.Now())
    
    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)