C/C++教程

sqlx: batch insert operation

本文主要是介绍sqlx: batch insert operation,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
func BatchAdd(ctx context.Context, listData []*DataTab) (int64, error) {
	operation := "batch_add_shipment_group_audit_log"
	sqlStatement := fmt.Sprintf("INSERT INTO %s (%s) VALUES ", TableName, ColumnNames)
	var values []interface{}
	for  _, row := range listData {
		sqlStatement += "(?,?,?,?,?),"
		values = append(values, row.field1, row.field2, row.field3)
	}
	result, err := db.Exec(operation, strings.TrimSuffix(sqlStatement, ","), values...)
	if err != nil {
		logCtx.Withs("operation", operation).WithError(err).Error("failed to BatchAdd")
		return 0, err
	}
	lastInsertId, err := result.LastInsertId()
	if err != nil {
		return 0, err
	}
	return lastInsertId, nil
}
这篇关于sqlx: batch insert operation的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!