如何在数据库插入后获取行值?

huangapple go评论91阅读模式
英文:

How to get row value(s) back after db insert?

问题

我正在使用Golang将数据插入到数据库中。基本上,我的查询如下所示:

var cols = "(col1, col2, col3)"
var values = "($1, $2, $3)"
var query = fmt.Sprintf("INSERT INTO %s %s VALUES %s", myTable, cols, values)

res, err := db.Exec(query, thing.val1, thing.val2, thing.val3)

res 只提供了 lastInsertId 和受影响的行数。但我需要的是受影响的行数。原因是我将数据插入到一个具有自增id列的psql数据库中,所以我希望能够返回该数据。

例如,使用Java Hibernate,我可以按照这个答案所解释的方式操作。我不需要重新查询数据库以获取ID。

编辑:我尝试使用 lastInsertId 方法,但出现了以下错误:

此驱动程序不支持 LastInsertId

英文:

I am using Golang to insert data into a DB. basically my query looks like below

var cols = "(col1, col2, col3)"
var values = "($1, $2, $3)"
var query = fmt.Sprintf("INSERT INTO %s %s VALUES %s", myTable, cols, values)

res, err := db.Exec(query, thing.val1, thing.val2, thing.val3)

The only things available from res are lastInsertId and # of rows affected. But what I need is the rows affected. The reason being is that I insert data into a psql database which has an AUTOINCREMENT id column - so I want the data back with that.

For example - with Java hibernate I can do what this answer explains. I don't have to re-query the DB for the ID.

EDIT: I tried to use the lastInsertId method and got this error
> LastInsertId is not supported by this driver

答案1

得分: 4

res.LastInsertId()在Postgres驱动程序中不受支持,但在MySQL驱动程序中受支持。

db.Exec()不返回最后插入的ID,但db.QueryRow()会返回。

为了更好地理解,您可以参考此链接

这里,我添加了一个可能会帮助您的示例。

var id int
err := db.QueryRow("INSERT INTO user (name) VALUES ('John') RETURNING id").Scan(&id)
if err != nil {
...
}
英文:

res.LastInsertId() is not supported in Postgres Driver. However, It is supported in MySQL Driver.

db.Exec() doesn't return last inserted id but db.QueryRow() does.

For better understanding you can refer this link.

Here, I added one example which might help you.

var id int
err := db.QueryRow("INSERT INTO user (name) VALUES ('John') RETURNING id").Scan(&id)
if err != nil {
...
}

答案2

得分: 2

假设您只想要在名为id的列中获取自动递增的值,并且这是使用pq驱动程序进行的插入操作。

单个插入的代码如下:

var cols = "(col1, col2, col3)"
var values = "($1, $2, $3)"
var query = fmt.Sprintf(
    "INSERT INTO %s %s VALUES %s RETURNING id",
    myTable, cols, values,
)
var id int
if err := db.QueryRow(
    query,
    thing.val1, thing.val2, thing.val3,
).Scan(&id); err != nil {
    panic(err)
}
fmt.Println("ID: ", id)

对于多个插入:

var cols = "(col1, col2, col3)"
var values = "($1, $2, $3),($4, $5, $6)"
var query = fmt.Sprintf(
    "INSERT INTO %s %s VALUES %s RETURNING id",
    myTable, cols, values,
)
var ids []int
rows, err := db.Query(
    query,
    thing.val1, thing.val2, thing.val3,
    thing.val4, thing.val5, thing.val6,
)
if err != nil {
    panic(err)
}
for rows.Next() {
    var id int
    if err := rows.Scan(&id); err != nil {
        panic(err)
    }
    ids = append(ids, id)
}
fmt.Println("IDs: ", ids)
英文:

Assuming you just want the auto-incremented value(s) in a column called id and this is an insert with the pq driver

var cols = "(col1, col2, col3)"
var values = "($1, $2, $3)"
var query = fmt.Sprintf(
    "INSERT INTO %s %s VALUES %s RETURNING id",
    myTable, cols, values,
)
var id int
if err := db.QueryRow(
    query,
    thing.val1, thing.val2, thing.val3,
).Scan(&id); err != nil {
    panic(err)
}
fmt.Println("ID: ", id)

For multiple inserts:

var cols = "(col1, col2, col3)"
var values = "($1, $2, $3),($4, $5, $6)"
var query = fmt.Sprintf(
    "INSERT INTO %s %s VALUES %s RETURNING id",
    myTable, cols, values,
)
var ids []int
rows, err := db.Query(
    query,
    thing.val1, thing.val2, thing.val3,
    thing.val4, thing.val5, thing.val6,
)
if err != nil {
    panic(err)
}
for rows.Next() {
    var id int
    if err := rows.Scan(&id); err != nil {
        panic(err)
    }
    ids = append(ids, id)
}
fmt.Println("IDs: ", ids)

huangapple
  • 本文由 发表于 2021年12月3日 08:25:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/70208076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定