-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
possible to get last inserted id? #97
Comments
Just use db.Exec and get the last inserted id from the result. In general db.Exec should be used for every CREATE / INSERT / UPDATE / DELETE. package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql", "user:pass@/dbname")
if err != nil {
panic("Error opening DB:", err.Error())
}
defer db.Close()
someParam := "value"
if res, err := db.Exec(`INSERT INTO foo VALUES("bar", ?))`, someParam)
if err != nil {
println("Exec err:", err.Error())
} else {
id, err := res.LastInsertId()
if err != nil {
println("Error:", err.Error())
} else {
println("LastInsertId:", id)
}
}
} See also: |
thanks sir! |
|
Ops typo. I fixed the code above. Thanks! |
How to convert int instead of int64?
|
It's a general Go newbie question. Don't ask such question here. |
after executing an insert, can get the last inserted id? this is a pretty common feature with mysql
The text was updated successfully, but these errors were encountered: