- 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)
推薦閱讀
- Puppet 4 Essentials(Second Edition)
- 現代C++編程:從入門到實踐
- JavaScript:Functional Programming for JavaScript Developers
- Learning Flask Framework
- SQL基礎教程(視頻教學版)
- QTP自動化測試進階
- Spring Boot企業級項目開發實戰
- Visual FoxPro程序設計
- Visual Basic程序設計
- Extending Puppet(Second Edition)
- QGIS 2 Cookbook
- Backbone.js Testing
- Three.js權威指南:在網頁上創建3D圖形和動畫的方法與實踐(原書第4版)
- 量子計算機編程:從入門到實踐
- Visual FoxPro程序設計習題及實驗指導