-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
63 lines (53 loc) · 1.28 KB
/
main.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
package main
import (
"database/sql"
"encoding/csv"
"fmt"
"io"
"log"
"os"
_ "github.com/mattn/go-sqlite3"
)
func main() {
os.Remove("config/sounds.db")
db, err := sql.Open("sqlite3", "config/sounds.db")
if err != nil {
log.Fatal("Unable to create database")
}
defer db.Close()
createStmt := `
create table if not exists sounds(command text, file text, played int);
`
_, err = db.Exec(createStmt)
if err != nil {
log.Fatalln("%q, %s\n", err, createStmt)
}
// lets load up our sounds
soundsFile, err := os.OpenFile("config/sounds.csv", os.O_RDWR|os.O_CREATE, os.ModePerm) // should figure out what these os objects are
if err != nil {
log.Fatalln(err)
}
defer soundsFile.Close()
reader := csv.NewReader(soundsFile)
//Configure reader options Ref http://golang.org/src/pkg/encoding/csv/reader.go?s=#L81
reader.Comma = ','
reader.Comment = '#'
reader.FieldsPerRecord = 2
reader.TrimLeadingSpace = true
for {
record, err := reader.Read()
// end-of-file is fitted into err
if err == io.EOF {
break
} else if err != nil {
fmt.Println("Error:", err)
reader.Read()
continue
}
insertStmt, err := db.Prepare("insert into sounds(command, file, played) values(?,?,?)")
if err != nil {
log.Fatal(err)
}
insertStmt.Exec(record[1], record[0], 0)
}
}