<pre><code>func BulkInsert(db *sql.DB, tableName string, unsavedRows []interface{}) error {
var query bytes.Buffer
query.WriteString("UPSERT INTO " + tableName + " (")
t := reflect.TypeOf(unsavedRows[0]) // assume whole collection is same type
valueArgs := make([]interface{}, 0)
for i := 0; i < t.NumField(); i++ {
query.WriteString(t.Field(i).Tag.Get("column"))
if i != t.NumField() -1 {
query.WriteString(",")
}
}
query.WriteString(") VALUES ")
fieldNum := 1
for i, post := range unsavedRows {
s := reflect.ValueOf(&post).Elem()
query.WriteString("(")
for j := 0; j < t.NumField(); j++ {
valueArgs = append(valueArgs, s.Elem().Field(j).Interface())
query.WriteString("$" + strconv.Itoa(fieldNum))
if j != t.NumField() - 1 {
query.WriteString(",")
}
fieldNum += 1
}
query.WriteString(")")
if i != len(unsavedRows) - 1 {
query.WriteString(",")
}
}
_, err := db.Exec(query.String(), valueArgs...)
return err
}
</code></pre>
<p>Assuming we have a struct with a tag like:</p>
<pre><code>type Cat struct {
Name string `column:"name"`
}
</code></pre>
<hr/>**评论:**<br/><br/>LogicX: <pre><p>if working with postgresql, this is not the most efficient way to load data.</p>
<p>Also Ikd review <a href="https://github.com/lukasmartinelli/pgfutter" rel="nofollow">pgfutter</a> which also happens to be coded in go.</p></pre>Tr1glav: <pre><p>What if I need to perform some data transformation prior to import? Would it be more efficient to do it, write it out to json/csv and then use this tool or simply call the function above? </p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传