INSERT datetime using now () with Go

Using sql database driver

I referenced the post above. But worrying about executing an INSERT statement, where one of my fields is for datetime. I tried to do "NOW" (), but when I check mysql db, I see 0000-00-00 00:00:00. Does anyone know what I can use as a value to correctly insert a date-time?

In addition, I know that I can build a datetime using a run-time package, but I do not want to use the Go machine’s runtime label. I want the machine datetime to start mysql. Hency, why did I use "NOW ()".

stmt, err := db.Prepare("INSERT userinfo SET username=?,departname=?,created=?")
checkErr(err)

res, err := stmt.Exec("reckhou", "IT", "NOW()")
checkErr(err)

id, err := res.LastInsertId()
checkErr(err)

fmt.Println(id)
+4
source share
2 answers

NOW() SQL, .

INSERT userinfo SET username=?,departname=?,created=NOW()

, MySQL "NOW()" , - 'foo'. MySQL DATETIME (MySQL , 'YYYY-MM-DD HH:MM:SS', . NULL, " ", .

+10

, .

var datetime = time.Now()
datetime.Format(time.RFC3339)
_, err = DatabaseHandle.Exec("INSERT into userinfo(id, date) VALUES (?,?)", 0, datetime)
+4

Source: https://habr.com/ru/post/1538910/


All Articles