使用golang从postgres中的csv批量插入而不使用for循环
Bulk insert from csv in postgres using golang without using for loop
我是 Golang 新手,
- 我有一个 csv 文件,其中的数据如下所示
field1,field2
1.1,2
1.2,3
1.3,2
- 我想在不使用 for 循环的情况下使用 golang 将所有 csv 数据插入 db table。我正在使用 postgres 数据库
也不想使用 sql 原始查询
我正在使用 gorm ORM
您可以为此使用 pgx 库:
filename := "foo.csv"
dbconn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
panic(err)
}
defer dbconn.Release()
f, err := os.Open(filename)
if err != nil {
panic(err)
}
defer func() { _ = f.Close() }()
res, err := dbconn.Conn().PgConn().CopyFrom(context.Background(), f, "COPY csv_test FROM STDIN (FORMAT csv)")
if err != nil {
panic(err)
}
fmt.Print(res.RowsAffected())
您可以将 CSV 解析为一段类型结构 using an external library, and then insert them all at once using GORM's batch insert。
这是一个例子:
import (
"os"
"github.com/gocarina/gocsv"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
// Entry defines both the CSV layout and database schema
type Entry struct {
gorm.Model
Field1 float64 `csv:"field1"`
Field2 float64 `csv:"field2"`
}
func main() {
// Open the CSV file for reading
file, err := os.Open("data.csv")
if err != nil {
panic(err)
}
defer file.Close()
// Parse CSV into a slice of typed data `[]Entry` (just like json.Unmarshal() does)
// The builtin package `encoding/csv` does not support unmarshaling into a struct
// so you need to use an external library to avoid writing for-loops.
var entries []Entry
err = gocsv.Unmarshal(file, &entries)
if err != nil {
panic(err)
}
// Open a postgres database connection using GORM
db, err := gorm.Open(postgres.Open("host=localhost user=postgres password=dev dbname=foo port=5432 sslmode=disable TimeZone=Europe/Paris"))
if err != nil {
panic(err)
}
// Create `entries` table if not exists
err = db.AutoMigrate(&Entry{})
if err != nil {
panic(err)
}
// Save all the records at once in the database
result := db.Create(entries)
if result.Error != nil {
panic(result.Error)
}
}
我是 Golang 新手,
- 我有一个 csv 文件,其中的数据如下所示
field1,field2
1.1,2
1.2,3
1.3,2
- 我想在不使用 for 循环的情况下使用 golang 将所有 csv 数据插入 db table。我正在使用 postgres 数据库
也不想使用 sql 原始查询
我正在使用 gorm ORM
您可以为此使用 pgx 库:
filename := "foo.csv"
dbconn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
panic(err)
}
defer dbconn.Release()
f, err := os.Open(filename)
if err != nil {
panic(err)
}
defer func() { _ = f.Close() }()
res, err := dbconn.Conn().PgConn().CopyFrom(context.Background(), f, "COPY csv_test FROM STDIN (FORMAT csv)")
if err != nil {
panic(err)
}
fmt.Print(res.RowsAffected())
您可以将 CSV 解析为一段类型结构 using an external library, and then insert them all at once using GORM's batch insert。
这是一个例子:
import (
"os"
"github.com/gocarina/gocsv"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
// Entry defines both the CSV layout and database schema
type Entry struct {
gorm.Model
Field1 float64 `csv:"field1"`
Field2 float64 `csv:"field2"`
}
func main() {
// Open the CSV file for reading
file, err := os.Open("data.csv")
if err != nil {
panic(err)
}
defer file.Close()
// Parse CSV into a slice of typed data `[]Entry` (just like json.Unmarshal() does)
// The builtin package `encoding/csv` does not support unmarshaling into a struct
// so you need to use an external library to avoid writing for-loops.
var entries []Entry
err = gocsv.Unmarshal(file, &entries)
if err != nil {
panic(err)
}
// Open a postgres database connection using GORM
db, err := gorm.Open(postgres.Open("host=localhost user=postgres password=dev dbname=foo port=5432 sslmode=disable TimeZone=Europe/Paris"))
if err != nil {
panic(err)
}
// Create `entries` table if not exists
err = db.AutoMigrate(&Entry{})
if err != nil {
panic(err)
}
// Save all the records at once in the database
result := db.Create(entries)
if result.Error != nil {
panic(result.Error)
}
}