-
Notifications
You must be signed in to change notification settings - Fork 12
/
test2.go
executable file
·85 lines (71 loc) · 1.79 KB
/
test2.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"github.com/jmoiron/sqlx"
// Needed to make the driver work.
_ "github.com/mattn/go-sqlite3"
)
func main() {
var db *sqlx.DB
var err error
// In memory sqlite3.
// Can also use connect to open and connect at the same time.
db, err = sqlx.Connect("sqlite3", "test2.db")
if err != nil {
panic(err)
}
defer db.Close()
// Create a simple table for phone numbers.
table1 := `CREATE TABLE phones(
phone text,
description text,
phoneID integer);`
_, err = db.Exec(table1)
// Returns an error if table already exists.
if err != nil {
fmt.Println(err)
}
// Add items to the phones table with bindvars.
addPhone := `INSERT INTO phones(phone, description, phoneID)
VALUES (?, ?, ?);`
// Now we can call it with values.
_, err = db.Exec(addPhone, "555-555-5555", "phone1", 1)
if err != nil {
fmt.Println(err)
}
// With Query, we can run a select and get everything from phones.
listPhones := `SELECT * FROM phones;`
rows, err := db.Query(listPhones)
if err != nil {
fmt.Println(err)
}
// Iterate over rows.
for rows.Next() {
var ph string
var de string
var id int
err = rows.Scan(&ph, &de, &id)
if err != nil {
fmt.Println(err)
}
// fmt.Println(ph, de, id)
}
// Can also iterate over structs with Queryx.
// Struct fields must be exported, otherwise we get an error.
// Use the tags to match table columns to fields if the names do not match.
rowx, err := db.Queryx(listPhones)
type phone struct {
Phone string `db:"phone"`
Desc string `db:"description"`
ID int `db:"phoneID"`
}
// Iterate over rows.
for rowx.Next() {
var p phone
err = rowx.StructScan(&p)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%+v\n", p)
}
}