postgresql - Golang增量数

标签 postgresql go

我不能在我的表上增加行数..
我使用了 revel 框架,并尝试制作简单的 crud。

问题是:“如何用 Golang 显示增量数?”。

这是我的 Controller 代码 book.go
这个 Controller 显示来自 postgresql 数据库的数据。

func allBooks() ([]models.Book, error) {
    //Retrieve
    books := []models.Book{}

    rows, err := db.Query("SELECT id, name, author, pages, publication_date FROM books order by id")
    defer rows.Close()
    if err == nil {
        for rows.Next() {
            var id int
            var name string
            var author string
            var pages int
            var publicationDate pq.NullTime

            err = rows.Scan(&id, &name, &author, &pages, &publicationDate)
            if err == nil {
                currentBook := models.Book{ID: id, Name: name, Author: author, Pages: pages}
                if publicationDate.Valid {
                    currentBook.PublicationDate = publicationDate.Time
                }

                books = append(books, currentBook)
            } else {
                return books, err
            }

        }
    } else {
        return books, err
    }
    return books, err
}  

还有这个 html 对我的看法 index.html:

<table class="table table-striped">
  <thead>
    <tr>
      <th>No</th>
      <th>ID</th>
      <th>Name</th>
      <th>Author</th>
      <th>Pages</th>
      <th>Publication Date</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>

    {{range .books}}
    <tr>
      <td>{{.Nomor}}</td>
      <td>{{.ID}}</td>
      <td>{{.Name}}</td>
      <td>{{.Author}}</td>
      <td>{{.Pages}}</td>
      <td>{{.PublicationDateStr}}</td>
    </tr>
    {{end}}
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td><a href="book.html" class="btn btn-primary">New book</a></td>
    </tr>
  </tbody>
</table>  

希望这样的结果

|No.|ID |Name |Author | Pages | Publication Date  |
|1  |2  |Jack |Holly  | 255   | 2017-10-15        |
|2  |4  |hans |Holly  | 255   | 2017-10-15        |
|3  |6  |Juri |Holly  | 255   | 2017-10-15        |
|4  |7  |Hank |Holly  | 255   | 2017-10-15        |

但我得到的是

|No.|ID |Name |Author | Pages | Publication Date | 
|0  |2  |Jack |Holly  | 255   | 2017-10-15       | 
|0  |4  |hans |Holly  | 255   | 2017-10-15       | 
|0  |6  |Juri |Holly  | 255   | 2017-10-15       | 
|0  |7  |Hank |Holly  | 255   | 2017-10-15       |

最佳答案

我在任何地方都看不到 Nomor 属性。

你应该保留一个计数器并做类似的事情

...
currentBook := models.Book{Nomor: count, ID: id, Name: name, Author: author, Pages: pages}
count = count + 1
...

关于postgresql - Golang增量数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46766791/

相关文章:

go - goreleaser仅在GitHub上生成变更日志

go - 你如何在 Go YAML 中编码换行符?

pointers - 将指针字段分配给强制转换的值

go - 生成后的模板标识

Postgresql 在 jsonb 对象中搜索值

python - 删除/创建与删除/更新的性能

python - 如何在 pandas/sql 中按分层数据分组?

javascript - 在 PostgreSQL 中存储 Javascript 数字的最佳方式是什么

sql - 创建一个 View ,其中 PostgreSQL 显示特定字段的前 100 行,然后移动到下一个

go - 使用代理连接到 Google Cloud SQL——错误 403 : Insufficient Permission