- Machine Learning With Go
- Daniel Whitenack
- 164字
- 2021-07-08 10:37:28
Modifying the database
As mentioned earlier, there is another flavor of interaction with the database called Exec. With these types of statements, we are concerned with updating, adding to, or otherwise modifying the state of one or more tables in the database. We use the same type of database connection, but instead of calling db.Query, we will call db.Exec.
For example, let's say we want to update some of the values in our iris database table:
// Update some values.
res, err := db.Exec("UPDATE iris SET species = 'setosa' WHERE species = 'Iris-setosa'")
if err != nil {
log.Fatal(err)
}
But how do we know whether we were successful and changed something? Well, the res function returned here allows us to see how many rows of our table were affected by our update:
// See how many rows where updated.
rowCount, err := res.RowsAffected()
if err != nil {
log.Fatal(err)
}
// Output the number of rows to standard out.
log.Printf("affected = %d\n", rowCount)
推薦閱讀
- Learning Selenium Testing Tools with Python
- PHP程序設計(慕課版)
- Nginx實戰:基于Lua語言的配置、開發與架構詳解
- Instant Nancy Web Development
- Mastering Git
- Python深度學習原理、算法與案例
- ASP.NET程序開發范例寶典
- 時空數據建模及其應用
- Kubernetes進階實戰
- 嵌入式Linux C語言程序設計基礎教程
- Modular Programming with JavaScript
- Cinder:Begin Creative Coding
- Mastering Data Analysis with R
- Mastering Swift 4(Fourth Edition)
- Learning RSLogix 5000 Programming