- Machine Learning With Go
- Daniel Whitenack
- 403字
- 2021-07-08 10:37:28
Querying the database
Now that we know how to connect to the database, let's see how we can get data out of the database. We won't cover the specifics of SQL queries and statements in this book. If you are not familiar with SQL, I would highly recommend that you learn how to query, insert, and so on, but for our purposes here, you should know that there are basically two types of operations we want to perform as related to SQL databases:
- A Query operation selects, groups, or aggregates data in the database and returns rows of data to us
- An Exec operation updates, inserts, or otherwise modifies the state of the database without an expectation that portions of the data stored in the database should be returned
As you might expect, to get data out of our database, we will use a Query operation. To do this, we need to query the database with an SQL statement string. For example, imagine we have a database storing a bunch of iris flower measurements (petal length, petal width, and so on), we could query some of that data related to a particular iris species as follows:
// Query the database.
rows, err := db.Query(`
SELECT
sepal_length as sLength,
sepal_width as sWidth,
petal_length as pLength,
petal_width as pWidth
FROM iris
WHERE species = $1`, "Iris-setosa")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
Note that this returns a pointer to an sql.Rows value, and we need to defer the closing of this rows value. Then we can loop over our rows and parse the data into values of expected type. We utilize the Scan method on rows to parse out the columns returned by the SQL query and print them to standard out:
// Iterate over the rows, sending the results to
// standard out.
for rows.Next() {
var (
sLength float64
sWidth float64
pLength float64
pWidth float64
)
if err := rows.Scan(&sLength, &sWidth, &pLength, &pWidth); err != nil {
log.Fatal(err)
}
fmt.Printf("%.2f, %.2f, %.2f, %.2f\n", sLength, sWidth, pLength, pWidth)
}
Finally, we need to check for any errors that might have occurred while processing our rows. We want to maintain the integrity of our data handling, and we cannot assume that we looped over all the rows without encountering an error:
// Check for errors after we are done iterating over rows.
if err := rows.Err(); err != nil {
log.Fatal(err)
}
- 從零開始構建企業(yè)級RAG系統(tǒng)
- Mastering JavaScript Functional Programming
- Android開發(fā)精要
- C語言程序設計實訓教程
- 微服務設計原理與架構
- The React Workshop
- Full-Stack Vue.js 2 and Laravel 5
- R的極客理想:工具篇
- 常用工具軟件立體化教程(微課版)
- C++寶典
- “笨辦法”學C語言
- HTML5移動前端開發(fā)基礎與實戰(zhàn)(微課版)
- Spring Data JPA從入門到精通
- SQL Server 2012 數(shù)據(jù)庫應用教程(第3版)
- C# 7.1 and .NET Core 2.0:Modern Cross-Platform Development(Third Edition)