Home > Mobile >  Datetime error while inserting data in mysql through golang
Datetime error while inserting data in mysql through golang

Time:05-24

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?

CodePudding user response:

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")
  • Related