-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestdb.go
More file actions
107 lines (87 loc) · 2.59 KB
/
Copy pathtestdb.go
File metadata and controls
107 lines (87 loc) · 2.59 KB
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
package main
/*
author:钱波
*/
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"net/http"
)
var (
userName string = "root"
password string = "root"
ipAddrees string = "localhost"
port int = 3306
dbName string = "test"
charset string = "utf8"
)
func connectMysql() (*sqlx.DB) {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s", userName, password, ipAddrees, port, dbName, charset)
Db, err := sqlx.Open("mysql", dsn)
if err != nil {
fmt.Printf("mysql connect failed, detail is [%v]", err.Error())
return nil
}
Db.SetMaxOpenConns(20)
Db.SetMaxIdleConns(15)
return Db
}
//返回最新的ID
func addRecord(Db *sqlx.DB)(userid int64) {
result, err := Db.Exec("insert into test_user(avatar,name) values(?,?)","qianbo", "123456")
if err != nil {
fmt.Printf("data insert faied, error:[%v]", err.Error())
return -1
}
id, _ := result.LastInsertId()
fmt.Printf("insert success, last id:[%d]\n", id)
return id
//for i:=0; i<2; i++ {//}
}
//更新数据
func updateRecord(Db *sqlx.DB, id int64){
result, err := Db.Exec("update test_user set name = 'test' where id = ?",id)
if err != nil {
fmt.Printf("update faied, error:[%v]", err.Error())
return
}
num, _ := result.RowsAffected()
fmt.Printf("update success, affected rows:[%d]\n", num)
}
func deleteRecord(Db *sqlx.DB,id int64){
result, err := Db.Exec("delete from test_user where id = ?",id)
if err != nil {
fmt.Printf("delete faied, error:[%v]", err.Error())
return
}
num, _ := result.RowsAffected()
fmt.Printf("delete success, affected rows:[%d]\n", num)
}
func IndexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "go lang server qianbo")
}
func userHandler_add(w http.ResponseWriter, r *http.Request){
}
func userHandler_update(w http.ResponseWriter, r *http.Request){
}
func userHandler_delete(w http.ResponseWriter, r *http.Request){
}
func userHandler_select(w http.ResponseWriter, r *http.Request){
fmt.Println("r.Method = ", r.Method)
fmt.Println("r.URL = ", r.URL)
fmt.Println("r.Header = ", r.Header)
fmt.Println("r.Body = ", r.Body)
fmt.Fprintf(w,"HelloWorld!")
}
func main() {
var Db *sqlx.DB = connectMysql()
defer Db.Close()
//addRecord(Db)
fmt.Println("server start at 9000 port")
http.HandleFunc("/", IndexHandler)
http.ListenAndServe("127.0.0.1:9000", nil)
//var id = addRecord(Db)
// updateRecord(Db,id)
// deleteRecord(Db,id)
}