英文:
Get Last record form my sqlite db using go
问题
我想查询我的sqlite数据库,以获取书籍表中的最后一个id,然后使用最后一个id + 1插入数据。我是Go的新手,我无法弄清楚如何设置我的lastid int变量。任何帮助将不胜感激。
func getBookLastID() int {
var lastid int
err := db.QueryRow("select max(id) from books").Scan(&lastid)
fmt.Println(lastid)
return lastid + 1
}
func insertBook(name, author string, pages int, publicationDate time.Time) (int, error) {
// 创建
var bookID int
lastid := getBookLastID()
err := db.QueryRow(`INSERT INTO books(id, name, author, pages, publication_date) VALUES($1, $2, $3, $4, $5)`, lastid, name, author, pages, publicationDate).Scan(&bookID)
if err != nil {
return 0, err
}
fmt.Printf("最后插入的ID:%v\n", bookID)
return bookID, err
}
以上是你要翻译的内容。
英文:
I want to query my sqlite db to get the last id in my book table then insert the data with the last id + 1. I am new to Go and I can't figure out how to set my lastid int variable. Any help would be greatly appreciated.
func getBookLastID() {
var lastid int
err := db.QueryRow("select max(id) from books").Scan(&lastid)
fmt.Println(lastid)
return (lastid + 1)
}
func insertBook(name, author string, pages int, publicationDate time.Time) (int, error) {
//Create
var bookID int
var lastid int
lastid = getBookLastID()
err := db.QueryRow(`INSERT INTO books(id, name, author, pages, publication_date) VALUES($1, $2, $3, $4, $5)`, lastid, name, author, pages, publicationDate).Scan(&bookID)
if err != nil {
return 0, err
}
fmt.Printf("Last inserted ID: %v\n", bookID)
return bookID, err
}
答案1
得分: 0
我在函数中缺少了"int"关键字。
错误的写法:
func getBookLastID() {....
正确的写法:
func getBookLastID() int {....
全部正确的代码如下:
func getBookLastID() int {
var id int
err := db.QueryRow("select ifnull(max(id), 0) as id from books").Scan(&id)
if err != nil {
panic(err)
}
fmt.Println("New record ID is:", id + 1)
return id + 1
}
func insertBook(name, author string, pages int, publicationDate time.Time) (int, error) {
// 创建
lastid := getBookLastID()
res, err := db.Exec(`INSERT INTO books(id, name, author, pages, publication_date) VALUES($1, $2, $3, $4, $5)`, lastid, name, author, pages, publicationDate)
if err != nil {
return 0, err
}
rowsCreated, err := res.RowsAffected()
if err != nil {
return 0, err
}
return int(rowsCreated), err
}
英文:
I was missing "int" in the func
Wrong:
func getBookLastID() {....
Correct:
func getBookLastID() int {....
Correct All:
func getBookLastID() int {
var id int
err := db.QueryRow("select ifnull(max(id), 0) as id from books").Scan(&id)
if err != nil {
panic(err)
}
fmt.Println("New record ID is:", id + 1)
return id + 1
}
func insertBook(name, author string, pages int, publicationDate time.Time) (int, error) {
//Create
lastid := getBookLastID()
res, err := db.Exec(`INSERT INTO books(id, name, author, pages, publication_date) VALUES($1, $2, $3, $4, $5)`, lastid, name, author, pages, publicationDate)
if err != nil {
return 0, err
}
rowsCreated, err := res.RowsAffected()
if err != nil {
return 0, err
}
return int(rowsCreated), err
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论