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
", bookID)
return bookID, err
}