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

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)
主站蜘蛛池模板: 广丰县| 白河县| 阿图什市| 麻阳| 左权县| 宁晋县| 华池县| 塘沽区| 庄河市| 娄烦县| 勃利县| 蛟河市| 东乌| 侯马市| 边坝县| 岳阳市| 济南市| 永清县| 宜良县| 土默特右旗| 平塘县| 延吉市| 兰州市| 商都县| 泸水县| 东莞市| 凤山市| 惠来县| 呈贡县| 喀喇沁旗| 山东省| 郸城县| 桂平市| 唐山市| 江山市| 崇文区| 南和县| 乐亭县| 西青区| 抚远县| 霍林郭勒市|