forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise1.go
65 lines (56 loc) · 1.36 KB
/
exercise1.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
64
65
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// go build
// ./exercise1
// 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
SUM(sepal_length) as sLength,
SUM(sepal_width) as sWidth,
SUM(petal_length) as pLength,
SUM(petal_width) as pWidth,
species
FROM iris
GROUP BY species`)
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
species string
)
if err := rows.Scan(&sLength, &sWidth, &pLength, &pWidth, &species); err != nil {
log.Fatal(err)
}
fmt.Printf("%s: %.2f, %.2f, %.2f, %.2f\n", species, sLength, sWidth, pLength, pWidth)
}
// Check for errors after we are done iterating over rows.
if err := rows.Err(); err != nil {
log.Fatal(err)
}
}