-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
106 lines (102 loc) · 2.24 KB
/
user.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
package model
import (
"fmt"
"ssevven/utils"
)
type User struct {
Uid int `json:"uid"`
Username string `json:"username"`
Password string `json:"password"`
}
//添加用户方法1//可以防止Sql注入?
func (user *User) AddUser() error {
//sql 语句 //注册
sqlStr := "insert into user(username,password) values(?,?)"
//预编译
inStmt, err := utils.Db.Prepare(sqlStr)
if err != nil {
fmt.Println("预编译处理异常=", err)
return err
}
//执行
_, err2 := inStmt.Exec(user.Username, user.Password)
if err2 != nil {
fmt.Println("执行出现异常=", err2)
return err2
}
return nil
}
func (user *User) GetUserByUsername() (*User, error) {
sqlStr := "select uid, username, password from user where username = ?"
//执行
row := utils.Db.QueryRow(sqlStr, user.Username)
//声明变量
var (
uid int
username string
password string
)
err := row.Scan(&uid, &username, &password)
if err != nil {
fmt.Println("查询出现异常=", err)
return nil, err
}
u := &User{
Uid: uid,
Username: username,
Password: password,
}
return u, nil
}
func (user *User) GetUserByUid() (*User, error) {
sqlStr := "select uid, username, password from user where uid = ?"
//执行
row := utils.Db.QueryRow(sqlStr, user.Uid)
//声明变量
var (
uid int
username string
password string
)
err := row.Scan(&uid, &username, &password)
if err != nil {
fmt.Println("查询出现异常=", err)
return nil, err
}
u := &User{
Uid: uid,
Username: username,
Password: password,
}
return u, nil
}
//获取所有的数据库用户
func (user *User) GetUsers() ([]*User, error) {
sqlStr := "select uid, username, password from user"
//执行
rows, err := utils.Db.Query(sqlStr)
if err != nil {
return nil, err
}
var users []*User
for rows.Next() {
//声明变量
var (
uid int
username string
password string
)
err := rows.Scan(&uid, &username, &password)
if err != nil {
fmt.Println("查询出现异常=", err)
return nil, err
}
u := &User{
Uid: uid,
Username: username,
Password: password,
}
users = append(users, u)
}
return users, nil
}