-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_mysql.go
143 lines (134 loc) · 3.01 KB
/
demo_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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package trygo
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql" // mysql driver
)
// TestMysql struct
type TestMysql struct {
db *sql.DB
}
// Init 初始化数据库引擎
func Init() (*TestMysql, error) {
test := new(TestMysql)
db, err := sql.Open("mysql", "test:test@tcp(127.0.0.1:3306)/abwork?charset=utf8")
//第一个参数 : 数据库引擎
//第二个参数 : 数据库DSN配置。Go中没有统一DSN,都是数据库引擎自己定义的,因此不同引擎可能配置不同
//本次演示采用http://code.google.com/p/go-mysql-driver
if err != nil {
fmt.Println("database initialize error : ", err.Error())
return nil, err
}
test.db = db
return test, nil
}
// Create 测试数据库数据添加
func (test *TestMysql) Create() {
if test.db == nil {
return
}
stmt, err := test.db.Prepare("insert into test(name,age)values(?,?)")
if err != nil {
fmt.Println(err.Error())
return
}
defer stmt.Close()
if result, err := stmt.Exec("张三", 20); err == nil {
if id, err := result.LastInsertId(); err == nil {
fmt.Println("insert id : ", id)
}
}
if result, err := stmt.Exec("李四", 30); err == nil {
if id, err := result.LastInsertId(); err == nil {
fmt.Println("insert id : ", id)
}
}
if result, err := stmt.Exec("王五", 25); err == nil {
if id, err := result.LastInsertId(); err == nil {
fmt.Println("insert id : ", id)
}
}
}
// Update 测试数据库数据更新
func (test *TestMysql) Update() {
if test.db == nil {
return
}
stmt, err := test.db.Prepare("update test set name=?,age=? where age=?")
if err != nil {
fmt.Println(err.Error())
return
}
defer stmt.Close()
if result, err := stmt.Exec("周七", 40, 25); err == nil {
if c, err := result.RowsAffected(); err == nil {
fmt.Println("update count : ", c)
}
}
}
/* 测试数据库数据读取 */
func (test *TestMysql) Read() {
if test.db == nil {
return
}
rows, err := test.db.Query("select id,name,age from test limit 0,5")
if err != nil {
fmt.Println(err.Error())
return
}
defer rows.Close()
fmt.Println("")
cols, _ := rows.Columns()
for i := range cols {
fmt.Print(cols[i])
fmt.Print("\t")
}
fmt.Println("")
var id int
var name string
var age int
for rows.Next() {
if err := rows.Scan(&id, &name, &age); err == nil {
fmt.Print(id)
fmt.Print("\t")
fmt.Print(name)
fmt.Print("\t")
fmt.Print(age)
fmt.Print("\t\r\n")
}
}
}
// Delete 测试数据库删除
func (test *TestMysql) Delete() {
if test.db == nil {
return
}
stmt, err := test.db.Prepare("delete from test where age=?")
if err != nil {
fmt.Println(err.Error())
return
}
defer stmt.Close()
if result, err := stmt.Exec(20); err == nil {
if c, err := result.RowsAffected(); err == nil {
fmt.Println("remove count : ", c)
}
}
}
// Close func
func (test *TestMysql) Close() {
if test.db != nil {
test.db.Close()
}
}
// DemoMysql func
func DemoMysql() {
if test, err := Init(); err == nil {
test.Create()
test.Update()
test.Read()
test.Delete()
test.Read()
test.Close()
}
}