- 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)
推薦閱讀
- Java面向對象思想與程序設計
- Flask Web開發入門、進階與實戰
- 神經網絡編程實戰:Java語言實現(原書第2版)
- Kinect for Windows SDK Programming Guide
- Visual C#.NET程序設計
- 零基礎學Python網絡爬蟲案例實戰全流程詳解(入門與提高篇)
- Java程序設計案例教程
- Learning Material Design
- Advanced UFT 12 for Test Engineers Cookbook
- iOS開發項目化入門教程
- C Primer Plus(第6版)中文版【最新修訂版】
- Mastering JavaScript Promises
- Python繪圖指南:分形與數據可視化(全彩)
- 計算機應用基礎
- OpenCV輕松入門:面向Python