在使用Golang向MySQL插入数据时出现了日期时间错误。

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

Datetime error while inserting data in mysql through golang

问题

获取此错误:在第1行的“cdate”列上,日期时间值不正确:'2022-05-23T20:51:48+05:30'

我的表格形式如下:

create table if not exists amplitude_enable (
cid int not null primary key, 
idc varchar(255), 
apiurl longtext, 
timezone varchar(255), 
status varchar(255), 
cdate datetime not null, 
udate datetime not null, 
primary_key varchar(255) not null, 
apikey varchar(255) not null
);

代码:

dt := time.Now().Format(time.RFC3339)
primary_key := "email" //function
queryStr := fmt.Sprintf("INSERT INTO amplitude_enable (cid, idc, apiurl, timezone, cdate, udate, primary_key, apikey) value(%f, '%s','%s','%s','%s','%s', '%s', '%s') on duplicate key update apiurl='%s', idc='%s', timezone='%s', udate='%s', apikey='%s'", cid.(float64), idc.(string), apiurl.(string), timezone.(string), dt, dt, primary_key, apikey.(string), apiurl.(string), idc.(string), timezone.(string), dt, apikey.(string))

如何修复此错误?

英文:

Getting this error:- Incorrect datetime value: '2022-05-23T20:51:48+05:30' for column 'cdate' at row 1

My table is of this form:-

create table if not exists amplitude_enable (
cid int not null primary key, 
idc varchar(255), 
apiurl longtext, 
timezone varchar(255), 
status varchar(255), 
cdate datetime not null, 
udate datetime not null, 
primary_key varchar(255) not null, 
apikey varchar(255) not null
);

Code:-

dt := time.Now().Format(time.RFC3339)
	primary_key := "email" //function
	queryStr := fmt.Sprintf("INSERT INTO amplitude_enable (cid, idc, apiurl, timezone, cdate, udate, primary_key, apikey) value(%f, '%s','%s','%s','%s','%s', '%s', '%s') on duplicate key update apiurl='%s', idc='%s', timezone='%s', udate='%s', apikey='%s'", cid.(float64), idc.(string), apiurl.(string), timezone.(string), dt, dt, primary_key, apikey.(string), apiurl.(string), idc.(string), timezone.(string), dt, apikey.(string))


How to fix this error?

答案1

得分: 2

对于 SQL 中的 DATETIME 类型,请使用格式 "2006-01-02 15:04:05"。但是考虑为了安全性,使用一些参数化的数据库访问或 ORM。

dt := time.Now().Format("2006-01-02 15:04:05")
英文:

For SQL use the format "2006-01-02 15:04:05" for DATETIME. But consider using some parameterized database access or ORM for safety.

dt := time.Now().Format("2006-01-02 15:04:05")

huangapple
  • 本文由 发表于 2022年5月23日 23:56:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/72351524.html
匿名

发表评论

匿名网友

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

确定