Confusion about LastInsertId() #1717
-
Hello @methane can you kindly clarify how this driver handles Note: I have confirmed that my database works as documented below when I run the queries directly. 1. Bulk Inserts
However, when I run the queries using this driver, the 2. When the ID is supplied
However, when I run the queries using this driver and call |
BetaWas this translation helpful?Give feedback.
Replies: 1 comment 1 reply
-
REALLY? // https://github.com/go-sql-driver/mysql/discussions/1717package main import ( "database/sql""fmt" _ "github.com/go-sql-driver/mysql" ) // Use example in https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_last-insert-idfuncmain(){db, err:=sql.Open("mysql", "root:my-secret-pw@tcp(127.0.0.1:3306)/test") iferr!=nil{panic(err) } res, err:=db.Exec("INSERT INTO t VALUES (NULL, 'Bob')") iferr!=nil{panic(err) } last, err:=res.LastInsertId() fmt.Printf("last insert id: %d\n", last) res, err=db.Exec("INSERT INTO t VALUES (NULL, 'Alice'),(NULL, 'Mary'), (NULL, 'Jane'), (NULL, 'Lisa')") iferr!=nil{panic(err) } last, err=res.LastInsertId() fmt.Printf("last insert id: %d\n", last) rows, err:=db.Query("SELECT id, name FROM t") iferr!=nil{panic(err) } deferrows.Close() forrows.Next(){varidintvarnamestringiferr:=rows.Scan(&id, &name); err!=nil{panic(err) } fmt.Printf("id: %d, name: %s\n", id, name) } iferr:=rows.Err(); err!=nil{panic(err) } }
This driver doesn't query
|
BetaWas this translation helpful?Give feedback.
REALLY?