Last active
May 30, 2017 20:41
-
-
Save amogh-plivo/0df5b056e90345729298fd39ca4bb2db to your computer and use it in GitHub Desktop.
How to insert multiple records in postgres using Gorm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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