-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
74 lines (57 loc) · 1.32 KB
/
data.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
package data
import (
"database/sql"
"log"
_ "github.com/mattn/go-sqlite3"
)
var db *sql.DB
func OpenDatabase() error {
var err error
db, err = sql.Open("sqlite3", "./sqlite-databse.db")
if err != nil {
return err
}
return db.Ping()
}
func CreateTable() {
createTableSQL := `CREATE TABLE IF NOT EXISTS studybuddy (
"Id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"word" TEXT,
"definition" TEXT,
"category" TEXT
);`
statement, err := db.Prepare(createTableSQL)
if err != nil {
log.Fatal(err.Error())
}
statement.Exec()
log.Println("Studybuddy table created")
}
func InsertNote(word string, definition string, category string) {
insertNoteSQL := `INSERT INTO studybuddy (word, definition, category)
VALUES (?, ?, ?)`
statement, err := db.Prepare(insertNoteSQL)
if err != nil {
log.Fatalln(err)
}
_, err = statement.Exec(word, definition, category)
if err != nil {
log.Fatalln(err)
}
log.Println("Inserted study note successfully")
}
func DisplayAllNotes() {
row, err := db.Query("SELECT * FROM studybuddy ORDER BY word")
if err != nil {
log.Fatalln(err)
}
defer row.Close()
for row.Next() {
var idNote int
var word string
var definition string
var category string
row.Scan(&idNote, &word, &definition, &category)
log.Println("[", category, "] ", word, "-", definition)
}
}