官术网_书友最值得收藏!

  • 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)
主站蜘蛛池模板: 苍梧县| 柳河县| 雅安市| 宣城市| 泗洪县| 灵寿县| 彭水| 大余县| 平原县| 时尚| 璧山县| 甘孜| 乌审旗| 隆德县| 南汇区| 清远市| 淄博市| 类乌齐县| 元朗区| 贵州省| 鄱阳县| 富宁县| 睢宁县| 峡江县| 吉安市| 泰宁县| 莎车县| 磐石市| 响水县| 凤冈县| 大方县| 城口县| 永新县| 丹东市| 克什克腾旗| 乳山市| 南康市| 安岳县| 鲁山县| 邮箱| 新宾|