Skip to content

Instantly share code, notes, and snippets.

@amogh-plivo
Last active May 30, 2017 20:41
Show Gist options
  • Save amogh-plivo/0df5b056e90345729298fd39ca4bb2db to your computer and use it in GitHub Desktop.
Save amogh-plivo/0df5b056e90345729298fd39ca4bb2db to your computer and use it in GitHub Desktop.
How to insert multiple records in postgres using Gorm
import "github.com/jinzhu/gorm"
databaseURL:= "user=abc password=abc dbname=db host=localhost port=5432"
db, err := gorm.Open("postgres", databaseURL)
if err != nil {
fmt.Printf("DBERR %s", err.Error())
}
db.DB().SetMaxIdleConns(10)
db.DB().SetMaxOpenConns(10)
db.LogMode(true)
m := &MessageDetailRecord{
AccountId: 123,
SubAccountId: 456,
}
query := "INSERT INTO Table(accountid, subaccountid) VALUES (?,?);"
a := time.Now()
tx := db.Begin()
for i:=0 ; i<100 ; i++ {
_ = tx.Exec(query, m.AccountId, m.SubAccountId).Error
}
tx.Commit()
delta := time.Now().Sub(a)
fmt.Println(delta.Nanoseconds())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment