简体   繁体   English

go mysql LAST_INSERT_ID() 返回 0

[英]go mysql LAST_INSERT_ID() returns 0

I have this MySQL database where I need to add records with a go program and need to retrieve the id of the last added record, to add the id to another table.我有这个 MySQL 数据库,我需要使用 go 程序添加记录,并且需要检索最后添加的记录的 id,以将 id 添加到另一个表。

When i run insert INSERT INTO table1 values("test",1); SELECT LAST_INSERT_ID()当我运行 insert INSERT INTO table1 values("test",1); SELECT LAST_INSERT_ID() INSERT INTO table1 values("test",1); SELECT LAST_INSERT_ID() in MySQL Workbench, it returns the last id, which is auto incremented, with no issues. INSERT INTO table1 values("test",1); SELECT LAST_INSERT_ID()在 MySQL 工作台中,它返回最后一个 id,它是自动递增的,没有问题。

If I run my go code however, it always prints 0. The code:但是,如果我运行我的 go 代码,它总是打印 0。代码:


    _, err := db_client.DBClient.Query("insert into table1 values(?,?)", name, 1)
    var id string
    err = db_client.DBClient.QueryRow("SELECT LAST_INSERT_ID()").Scan(&id)
    if err != nil {
        panic(err.Error())
    }
    fmt.Println("id: ", id)

I tried this variation to try to narrow down the problem scope further: err = db_client.DBClient.QueryRow("SELECT id from table1 where name=\"pleasejustwork\";").Scan(&id) , which works perfectly fine;我尝试了这种变体以尝试进一步缩小问题 scope 的范围: err = db_client.DBClient.QueryRow("SELECT id from table1 where name=\"pleasejustwork\";").Scan(&id) ,它工作得很好; go returns the actual id. go 返回实际 id。

Why is it not working with the LAST_INSERT_ID()?为什么它不能与 LAST_INSERT_ID() 一起使用?

I'm a newbie in go so please do not go hard on me if i'm making stupid go mistakes that lead to this error:D我是 go 的新手,所以如果我犯了导致此错误的愚蠢 go 错误,请不要对我强硬:D

Thank you in advance.先感谢您。

The MySQL protocol returns LAST_INSERT_ID() values in its response to INSERT statements. MySQL 协议在其对INSERT语句的响应中返回LAST_INSERT_ID()值。 And, the golang driver exposes that returned value.而且,golang 驱动程序公开了该返回值。 So, you don't need the extra round trip to get it.因此,您不需要额外的往返行程即可获得它。 These ID values are usually unsigned 64-bit integers.这些 ID 值通常是无符号的 64 位整数。

Try something like this.尝试这样的事情。

    res, err := db_client.DBClient.Exec("insert into table1 values(?,?)", name, 1)
     if err != nil {
        panic (err.Error())
    }
    id, err := res.LastInsertId()
    if err != nil {
        panic (err.Error())
    }
    fmt.Println("id: ", id)

I confess I'm not sure why your code didn't work.我承认我不确定为什么你的代码不起作用。 Whenever you successfully issue a single-row INSERT statement, the next statement on the same database connection always has access to a useful LAST_INSERT_ID() value.每当您成功发出单行 INSERT 语句时,同一数据库连接上的下一条语句始终可以访问有用的LAST_INSERT_ID()值。 This is true whether or not you use explicit transactions.无论您是否使用显式事务都是如此。

But if your INSERT is not successful, you must treat the last insert ID value as unpredictable.但是如果您的INSERT不成功,您必须将最后一个插入 ID 值视为不可预测。 (That's a technical term for "garbage", trash, rubbish, basura, etc.) (这是“垃圾”,垃圾,垃圾,basura等的技术术语。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM