-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
48 lines (38 loc) · 754 Bytes
/
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
package main
import (
"database/sql"
"os"
)
type Database struct {
db *sql.DB
}
func (d *Database) Destroy() error {
return os.Remove("./users.db")
}
func (d *Database) Setup() error {
db, err := sql.Open("sqlite3", "./users.db")
if err != nil {
return err
}
d.db = db
statement, err := db.Prepare(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
username varchar(128)
)
`)
if err != nil {
return err
}
_, err = statement.Exec()
return err
}
// AddUser adds a user with the given username.
func (d *Database) AddUser(username string) error {
statement, err := d.db.Prepare("INSERT INTO users (username) VALUES (?)")
if err != nil {
return err
}
_, err = statement.Exec(username)
return err
}