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

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)
主站蜘蛛池模板: 五寨县| 贡嘎县| 砚山县| 泽州县| 云林县| 西藏| 聂荣县| 鹤庆县| 宣恩县| 佛冈县| 玉门市| 揭西县| 同心县| 宁陕县| 寿光市| 长海县| 无棣县| 溧水县| 宜昌市| 阿坝| 新乐市| 开封市| 马山县| 霍邱县| 罗田县| 衡山县| 灵宝市| 珲春市| 寿宁县| 定西市| 枝江市| 门头沟区| 新巴尔虎左旗| 潮安县| 和林格尔县| 宜兴市| 从江县| 资溪县| 上高县| 卫辉市| 成安县|