Open xaququ opened 6 years ago
I don't think there is a problem here. For example, c1 database field is 'date'. And you have asked ODBC convert that date into string (by supplying string c1 variable to rows.Scan). And ODBC driver converted date into '2017-12-27T00:00:00+08:00'. Why do you think converting to '2017-12-27' string (what mysql driver did) is better than converting into '2017-12-27T00:00:00+08:00' string? If you want to control the conversion, you should use c1 of time.Time type and then convert it into whatever string you need.
Please let me know if I am missing something here. Thank you.
Alex
--------correct with mysql driver-------------- c1=2017-12-27 c2=10:12:59 c3=2017-12-27 10:12:59 c4=2017-12-31 23:59:59 -------incorrect with odbc driver-------------- c1=2017-12-27T00:00:00+08:00 c2=0001-01-01T10:12:59+08:00 c3=2017-12-27T10:12:59+08:00 c4=2017-12-31T23:59:59+08:00
test case as following
package main
import ( "database/sql" "fmt"
)
func main() { conn, err := sql.Open("odbc", "DSN=mysql;UID=root;PWD=mysql") //conn, err := sql.Open("mysql", "root:mysql@/mysql") conn.Exec("create table timeformat(c1 date,c2 time,c3 timestamp,c4 datetime)") conn.Exec("insert into timeformat values('2017-12-27','10:12:59','2017-12-27 10:12:59','2017-12-31 23:59:59')") rows, err := conn.Query("select * from timeformat") if err != nil { panic(err) } var c1, c2, c3, c4 string for rows.Next() { err = rows.Scan(&c1, &c2, &c3, &c4) fmt.Printf("c1=%v\nc2=%v\nc3=%v\nc4=%v\n", c1, c2, c3, c4) } conn.Exec("drop table timeformat") conn.Close() }