-
Notifications
You must be signed in to change notification settings - Fork 7
/
person.go
104 lines (72 loc) · 2.28 KB
/
person.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
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/mattn/go-sqlite3"
)
type person struct {
id int
first_name string
last_name string
email string
ip_address string
}
func addPerson(db *sql.DB, newPerson person) {
stmt, _ := db.Prepare("INSERT INTO people (id, first_name, last_name, email, ip_address) VALUES (?, ?, ?, ?, ?)")
stmt.Exec(nil, newPerson.first_name, newPerson.last_name, newPerson.email, newPerson.ip_address)
defer stmt.Close()
fmt.Printf("Added %v %v \n", newPerson.first_name, newPerson.last_name)
}
func searchForPerson(db *sql.DB, searchString string) []person {
rows, err := db.Query("SELECT id, first_name, last_name, email, ip_address FROM people WHERE first_name like '%" + searchString + "%' OR last_name like '%" + searchString + "%'")
defer rows.Close()
err = rows.Err()
if err != nil {
log.Fatal(err)
}
people := make([]person, 0)
for rows.Next() {
ourPerson := person{}
err = rows.Scan(&ourPerson.id, &ourPerson.first_name, &ourPerson.last_name, &ourPerson.email, &ourPerson.ip_address)
if err != nil {
log.Fatal(err)
}
people = append(people, ourPerson)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
return people
}
func getPersonById(db *sql.DB, ourID string) person {
fmt.Printf("Value is %v", ourID)
rows, _ := db.Query("SELECT id, first_name, last_name, email, ip_address FROM people WHERE id = '" + ourID + "'")
defer rows.Close()
ourPerson := person{}
for rows.Next() {
rows.Scan(&ourPerson.id, &ourPerson.first_name, &ourPerson.last_name, &ourPerson.email, &ourPerson.ip_address)
}
return ourPerson
}
func updatePerson(db *sql.DB, ourPerson person) int64 {
stmt, err := db.Prepare("UPDATE people set first_name = ?, last_name = ?, email = ?, ip_address = ? where id = ?")
checkErr(err)
defer stmt.Close()
res, err := stmt.Exec(ourPerson.first_name, ourPerson.last_name, ourPerson.email, ourPerson.ip_address, ourPerson.id)
checkErr(err)
affected, err := res.RowsAffected()
checkErr(err)
return affected
}
func deletePerson(db *sql.DB, idToDelete string) int64 {
stmt, err := db.Prepare("DELETE FROM people where id = ?")
checkErr(err)
defer stmt.Close()
res, err := stmt.Exec(idToDelete)
checkErr(err)
affected, err := res.RowsAffected()
checkErr(err)
return affected
}