- 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)
推薦閱讀
- DevOps:軟件架構師行動指南
- Intel Galileo Essentials
- Modular Programming with Python
- Moodle Administration Essentials
- Learning PostgreSQL
- Learn to Create WordPress Themes by Building 5 Projects
- Vue.js前端開發基礎與項目實戰
- Java網絡編程核心技術詳解(視頻微課版)
- JBoss:Developer's Guide
- Julia數據科學應用
- 貫通Tomcat開發
- Learning Unreal Engine Game Development
- SQL Server 2008實用教程(第3版)
- Mastering Python
- Mastering ASP.NET Web API