dongyi5570 2014-05-01 20:01
浏览 570

在Go中使用now()插入日期时间

Using sql database driver

I've been referencing the post above. But having troubling doing an INSERT statement where one of my fields are for datetime. I tried doing "NOW()" but when I check the mysql db, i am seeing 0000-00-00 00:00:00. Anyone know what I can use as the value to properly have it insert the datetime?

Also- I know I can construct datetime using go's time package, but I do not wish to use the timestamp of the machine running Go. I want the datetime of the machine running mysql. Hency, why I used "NOW()".

stmt, err := db.Prepare("INSERT userinfo SET username=?,departname=?,created=?")
checkErr(err)

res, err := stmt.Exec("reckhou", "IT", "NOW()")
checkErr(err)

id, err := res.LastInsertId()
checkErr(err)

fmt.Println(id)
  • 写回答

2条回答 默认 最新

  • duanpoqiu0919 2014-05-01 20:12
    关注

    The NOW() needs to be part of the SQL text, it's not a bind value.

    INSERT userinfo SET username=?,departname=?,created=NOW()
    

    The way you have it coded, MySQL is seeing "NOW()" as a literal string, as if it were any old string like 'foo'. MySQL is casting that string value to a DATETIME (MySQL is expecting a format close to 'YYYY-MM-DD HH:MM:SS', and it's failing to cast that to a valid date. We'd expect either a NULL or a special "zero-date" like you are seeing.

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?