无法使用Golang更新MySQL中的时间戳列。

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

Cannot update the timestamp column in mysql using golang

问题

我在我的应用程序中使用golang。我使用beego框架来创建它。我使用beego ORM来进行数据库操作。我执行以下操作:

 num, err := o.Raw("UPDATE apply_leave SET leavestatus=?,resultdate=?
 WHERE leaveid=?",leaveResult.LeaveResult, time.Now(),leave_id).Exec()

当我运行这个代码时,我得到以下错误:

"Error 1292: Incorrect datetime value: '15:46:59' for column 'resultdate' at row 1"

请注意,resultdate的类型是timestamp。感谢任何帮助...

英文:

I am using golang in my application.I m using beego framework to create it.I use beego ORM to do the database operations.I do the following

 num, err := o.Raw("UPDATE apply_leave SET leavestatus=?,resultdate=?
 WHERE leaveid=?",leaveResult.LeaveResult, time.Now(),leave_id).Exec()

When i run this i m getting the following error

"Error 1292: Incorrect datetime value: '15:46:59' for column 'resultdate' at row 1"

Please do note that the resultdate is of type timestamp.Appreciate any help...

答案1

得分: 1

你可以将时间格式化为"2006-01-02 15:04:05",并明确指定格式。

将你的代码修改如下可以帮助解决问题:

const MySQLTimeFormat = "2006-01-02 15:04:05"
num, err := o.Raw("UPDATE apply_leave SET leavestatus=?,resultdate=?
 WHERE leaveid=?", leaveResult.LeaveResult, time.Now().Format(MySQLTimeFormat), leave_id).Exec()
英文:

You may format it as format time as "2006-01-02 15:04:05" explicitly

Changing your code as follows will help

const MySQLTimeFormat = "2006-01-02 15:04:05"
num, err := o.Raw("UPDATE apply_leave SET leavestatus=?,resultdate=?
 WHERE leaveid=?",leaveResult.LeaveResult, time.Now().Format(MySQLTimeFormat),leave_id).Exec()

huangapple
  • 本文由 发表于 2017年1月12日 15:53:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/41607765.html
匿名

发表评论

匿名网友

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

确定