在go语言中使用GORM或SQL驱动程序读取时间戳时报错:Scan error on column index ...

2023-06-01 00:00:00 读取 驱动程序 时报

现在使用应用程序从数据库中读取数据很常见,有时数据库连接配置中缺少参数会导致混乱。

例如,在尝试从数据库中读取时间戳数据时,你会遇到这些错误:

sql: Scan error on column index 2: unsupported driver -> Scan pair: []uint8 -> *time.Time


当您尝试从数据库(MySQL、SQLite 等)中读取时间戳时,

它将变为:

0001-01-01 00:00:00 +0000 UTC


解决方案:

确保使用parseTime=true参数初始化您的数据库连接。

例如:

dbConn, err := gorm.Open("mysql", "username:[email protected](xx.xx0.x65.xx4:3306)/dbname?charset=utf8mb4&parseTime=true&loc=Local")


测试了一下是否正常了


其他问题:

1.写入的时间总是比实际的时间晚8个小时,而将时间在Go执行是打印出来的时候又是正常的,该问题也是时区参数问题

解决方式:

也是添加上时区参数parseTime=true&loc=Local

相关文章