我再次尝试将大量 csv 数据推送到 postgres 数据库中。
过去,我创建了一个结构来保存数据,并在将数据放入数据库表之前将每一列解压缩到结构中,这工作正常,但是,我刚刚找到 pgx.CopyFrom* 并且看起来好像我应该能够让它更好地工作。
到目前为止,我已经将表的列标题放入一段字符串中,将 csv 数据放入另一段字符串中,但我无法计算出将其推入数据库的语法。
我发现这篇文章有点像我想要的,但使用 [][]interface{} 而不是 []strings。
我到目前为止的代码是
// loop over the lines and find the first one with a timestamp
for {
line, err := csvReader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Error("Error reading csv data", "Loading Loop", err)
}
// see if we have a string starting with a timestamp
_, err := time.Parse(timeFormat, line[0])
if err == nil {
// we have a data line
_, err := db.CopyFrom(context.Background(), pgx.Identifier{"emms.scada_crwf.pwr_active"}, col_headings, pgx.CopyFromRows(line))
}
}
}
但是 pgx.CopyFromRows 期望 [][]interface{} 而不是 []string。
语法应该是什么?我是不是找错树了?
温温酱
翻过高山走不出你
随时随地看视频慕课网APP
相关分类