使用pgx.copyfrom插入Postgres数据库的批量插入CSV数据
我再次尝试将大量CSV数据推入Postgres数据库。
在过去好像我应该能够使它变得更好。
到目前为止,我已经将表的列标题放到了一片字符串中,将CSV数据放入了另一片字符串中,但我无法算出语法将其推入数据库。
我找到了做我想要的,但使用[] []界面{}而不是[]字符串。
到目前为止,我拥有的代码是
// 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 Expect [] []接口{}不是[]字符串。
语法应该是什么?我在吠叫错误的树吗?
I'm once again trying to push lots of csv data into a postgres database.
In the past I've created a struct to hold the data and unpacked each column into the struct before bumping the lot into the database table, and that is working fine, however, I've just found pgx.CopyFrom* and it would seem as though I should be able to make it work better.
So far I've got the column headings for the table into a slice of strings and the csv data into another slice of strings but I can't work out the syntax to push this into the database.
I've found this post which sort of does what I want but uses a [][]interface{} rather than []strings.
The code I have so far is
// 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))
}
}
}
But pgx.CopyFromRows expects [][]interface{} not []string.
What should the syntax be? Am I barking up the wrong tree?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议阅读您的CSV并为您阅读的每个记录创建
[] interface {}
,将[] interface {}
附加到行集合([
[ ] []接口{}
),然后将行传递到PGX。我无法模拟整个程序,但这是将CSV转换为
[] [] [] interface {}
, https://go.dev/play/p/efbifn2fjmi 。并在文档中签入, https://pkg.go.do.dev/github/github .com/jackc/pgx/v4 。
I recommend reading your CSV and creating a
[]interface{}
for each record you read, appending the[]interface{}
to a collection of rows ([][]interface{}
), then passing rows on to pgx.I can't mock up the entire program, but here's a full demo of converting the CSV to
[][]interface{}
, https://go.dev/play/p/efbiFN2FJMi.And check in with the documentation, https://pkg.go.dev/github.com/jackc/pgx/v4.