-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
58 lines (52 loc) · 1.12 KB
/
db.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
package main
import (
"database/sql"
"log"
"strconv"
"time"
_ "github.com/mattn/go-sqlite3"
)
func addTodo(db *sql.DB, title string) {
tx, _ := db.Begin()
current_time := time.Now()
stmt, _ := tx.Prepare("insert into toDo (title,create_time) values (?,?)")
_, err := stmt.Exec(title, current_time)
if err != nil {
log.Fatal(err)
}
tx.Commit()
}
func getTodos(db *sql.DB) [][]string {
rows, err := db.Query("select * from toDo")
if err != nil {
log.Fatal(err)
}
todos := [][]string{}
for rows.Next() {
var tempTodo toDo_model
err =
rows.Scan(&tempTodo.id, &tempTodo.title, &tempTodo.created_at)
if err != nil {
log.Fatal(err)
}
todos = append(todos, []string{strconv.Itoa(tempTodo.id), tempTodo.title, tempTodo.created_at.Format("2 Jan 2006 15:04")})
}
return todos
}
func deleteTodo(db *sql.DB, id string) {
tx, _ := db.Begin()
stmt, _ := tx.Prepare("DELETE FROM toDo WHERE id=?")
_, err := stmt.Exec(id)
if err != nil {
log.Fatal(err)
}
tx.Commit()
}
func deleteAllTodo(db *sql.DB) {
tx, _ := db.Begin()
_, err := tx.Exec("DELETE FROM toDo;")
if err != nil {
log.Fatal(err)
}
tx.Commit()
}