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

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)
主站蜘蛛池模板: 华安县| 大丰市| 拜城县| 崇明县| 萝北县| 乌拉特前旗| 绩溪县| 高清| 体育| 米林县| 普格县| 大丰市| 盐源县| 哈尔滨市| 方正县| 尼勒克县| 堆龙德庆县| 江陵县| 新沂市| 琼结县| 罗源县| 墨脱县| 韶山市| 普陀区| 水城县| 收藏| 伊金霍洛旗| 钦州市| 山丹县| 天台县| 南昌县| 田阳县| 天全县| 天水市| 西丰县| 上杭县| 永修县| 张家界市| 华阴市| 陇川县| 宝兴县|