-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.go
73 lines (64 loc) · 1.16 KB
/
notes.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
package main
import (
"fmt"
"log"
_ "github.com/mattn/go-sqlite3"
)
var Notes *[]Note
var title string
type Note struct{
Title string `json:"title"`
Cont string `json:"content"`
}
// Update updates note content
func (nt Note) Update(_title string,_cont string){
nt.Title = _title
nt.Cont = _cont
}
func (nt Note) Save(){
tx, err := DB.Begin()
if err != nil {
fmt.Printf("tx %v",err)
}
stmt, err := tx.Prepare("insert into Notes(title, content) values(?, ?)")
if err != nil {
fmt.Printf("tx %v",err)
}
defer stmt.Close()
_, err = stmt.Exec(nt.Title,nt.Cont)
if err != nil {
log.Println(err)
}
tx.Commit()
}
func (nt Note)GetAll(){
rows, err := DB.Query("select id, title, content from Notes")
if err != nil {
log.Println(err)
}
defer rows.Close()
for rows.Next() {
var id int
var content string
err = rows.Scan(&id, &title, &content)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%d | %s:\n",id, title)
fmt.Println(content)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
}
func DeleteAll(){
sqlStmt := `
drop table Notes;
`
_, err := DB.Exec(sqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, sqlStmt)
return
}
}