-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_mysql.go
46 lines (41 loc) · 974 Bytes
/
hello_mysql.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
package main
import (
"database/sql"
"log"
_"github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql", "root:P4Ssw0rD!@tcp(172.20.0.2:3306)/test")
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec(
"CREATE TABLE IF NOT EXISTS test.hello(world varchar(50))")
if err != nil {
log.Fatal(err)
}
res, err := db.Exec(
"INSERT INTO test.hello(world) VALUES('hello world!')")
if err != nil {
log.Fatal(err)
}
rowCount, err := res.RowsAffected()
if err != nil {
log.Fatal(err)
}
log.Printf("inserted %d rows", rowCount)
rows, err := db.Query("SELECT * FROM test.hello")
if err != nil {
log.Fatal(err)
}
for rows.Next() {
var s string
err = rows.Scan(&s)
if err != nil {
log.Fatal(err)
}
log.Printf("found row containing %q", s)
}
rows.Close()
}