我的要求是通过database/sql pkg向oracle表中插入多行。下面是我的代码片段,我一直在尝试:
db, err := sql.Open("oracle", connectionUrl)
if err != nil {
e.logger.Error("Can't create connection: " + err.Error())
return err
}
valueStrings := make([]string, 0, len(metrics))
valueArgs := make([]interface{}, 0, len(metrics)*9)
for _, post := range metrics {
valueStrings = append(valueStrings, "(?, ?, ?, ?, ?, ?, ?, ?, ?)")
valueArgs = append(valueArgs, post.Timestamp)
valueArgs = append(valueArgs, post.MetricName)
valueArgs = append(valueArgs, post.MetricType)
valueArgs = append(valueArgs, post.MetricUnit)
valueArgs = append(valueArgs, post.MetricDescription)
valueArgs = append(valueArgs, post.MetricValue)
valueArgs = append(valueArgs, post.Host)
valueArgs = append(valueArgs, post.ResourceAttributes)
valueArgs = append(valueArgs, post.MetricAttributes)
}
sql := fmt.Sprintf("INSERT INTO my_table(TS, NAME, TYPE, UNIT, DESCRIPTION, VALUE, HOST, RESOURCE_ATTRIBUTES, METRIC_ATTRIBUTES) VALUES %s",
strings.Join(valueStrings, ","))
logger.Info("#### Insert sql : " + sql)
logger.Info("### ValueArgs :")
fmt.Println(valueArgs)
result, err := db.Exec(sql, valueArgs...)
if err != nil {
e.logger.Error("Bulk insert failed: " + err.Error())
return err
}
我可以看到数据库连接成功,SQL成功创建为:
INSERT INTO my_table(TS, NAME, TYPE, UNIT, DESCRIPTION, VALUE, HOST, RESOURCE_ATTRIBUTES, METRIC_ATTRIBUTES) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?),(?, ?, ?, ?, ?, ?, ?, ?, ?),(?, ?, ?, ?, ?, ?, ?, ?, ?),(?, ?, ?, ?, ?, ?, ?, ?, ?),(?, ?, ?, ?, ?, ?, ?, ?, ?)
但是当执行带有valueArgs的sql时,它返回错误。完全不知道问题出在哪里。错误日志为:大容量插入失败:EOF {“kind”:“exporter”,“data_type”:“metrics”,“name”:“oracle”}有什么建议可以修改上面的代码并使其正常工作吗?
1条答案
按热度按时间41zrol4v1#
在Oracle版本23之前,
INSERT INTO ... VALUES (...),(...),(...)
不是有效语法。您可以使用INSERT INTO ... SELECT ...
或INSERT ALL ...
举例来说:
和
看看这个数组插入示例,看起来你可以使用如下内容: