通过 golang 在 mysql 中插入数据时出现日期时间错误
Datetime error while inserting data in mysql through golang
出现此错误:- 日期时间值不正确:第 1 行 'cdate' 的“2022-05-23T20:51:48+05:30”
我的table是这样的形式:-
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))
如何解决这个错误?
对于 SQL 使用格式 "2006-01-02 15:04:05"
对于 DATETIME
。但考虑使用一些参数化数据库访问或 ORM 以确保安全。
dt := time.Now().Format("2006-01-02 15:04:05")
出现此错误:- 日期时间值不正确:第 1 行 'cdate' 的“2022-05-23T20:51:48+05:30”
我的table是这样的形式:-
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))
如何解决这个错误?
对于 SQL 使用格式 "2006-01-02 15:04:05"
对于 DATETIME
。但考虑使用一些参数化数据库访问或 ORM 以确保安全。
dt := time.Now().Format("2006-01-02 15:04:05")