英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论