Get Last record form my sqlite db using go

huangapple go评论85阅读模式
英文:

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

}

huangapple
  • 本文由 发表于 2017年6月13日 22:11:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/44524001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定