forked from ardanlabs/gotraining
/
example3.go
63 lines (54 loc) · 1.3 KB
/
example3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// go build
// ./example3
// Sample program to retrieve results from a database.
package main
import (
"database/sql"
"fmt"
"log"
// go-sqlite3 is the libary that allows us to connect
// to sqlite with databases/sql.
_ "github.com/mattn/go-sqlite3"
)
func main() {
// Open a database value. Specify the sqlite3 driver
// for databases/sql.
db, err := sql.Open("sqlite3", "../data/iris.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// 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 = ?`, "Iris-setosa")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// 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)
}
// Check for errors after we are done iterating over rows.
if err := rows.Err(); err != nil {
log.Fatal(err)
}
}