我正在尝试使用 sqlx 和 golang 进行批量插入:
for _, result := range results {
queryInsert := `INSERT INTO "DataCom_travel" (com1,com2,path,time) VALUES ($1,$2,$3,$4)`
db.MustExec(queryInsert,result.Com1,result.Com2,result.Path,result.Time)
queryUpdate := `UPDATE "DataCom_commcombinaison" set done = TRUE WHERE com1 =$1 and com2 =$2`
db.MustExec(queryUpdate,result.Com1,result.Com2)
}
该代码有效,但速度很慢。
我试过这个:
tx := db.MustBegin()
for _, result := range results {
queryInsert := `INSERT INTO "DataCom_travel" (com1,com2,path,time) VALUES ($1,$2,$3,$4)`
tx.MustExec(queryInsert,result.Com1,result.Com2,result.Path,result.Time)
queryUpdate := `UPDATE "DataCom_commcombinaison" set done = TRUE WHERE com1 =$1 and com2 =$2`
tx.MustExec(queryUpdate,result.Com1,result.Com2)
}
tx.Commit()
但是当我查看我的记录时它什么也没做,我没有看到任何记录。
问候
编辑 :
INSERT INTO "DataCom_travel" (com1,com2,path,time) VALUES ($1,$2,$3,$4),($2,$3,$4,$5),($3,$4,$5,$6),($4,$5,$6,$7),($5,$6,$7,$8),($6,$7,$8,$9),($7,$8,$9,$10),($8,$9,$10,$11),($9,$10,$11,$12),($10,$11,$12,$13)
UPDATE "DataCom_commcombinaison" set done = TRUE WHERE (com1 = $1 AND com2 = $2 ) OR (com1 = $2 AND com2 = $3 ) OR (com1 = $3 AND com2 = $4 ) OR (com1 = $4 AND com2 = $5 ) OR (com1 = $5 AND com2 = $6 ) OR (com1 = $6 AND com2 = $7 ) OR (com1 = $7 AND com2 = $8 ) OR (com1 = $8 AND com2 = $9 ) OR (com1 = $9 AND com2 = $10 ) OR (com1 = $10 AND com2 = $11 )
panic: pq: column "com2" is of type integer but expression is of type text