/
db.go
70 lines (58 loc) · 1.29 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
59
60
61
62
63
64
65
66
67
68
69
70
package monitor
import (
"os"
"fmt"
"gosqlite.googlecode.com/hg/sqlite"
)
type Asin struct {
id int
asin string
}
type Fetching struct {
asin *Asin
dt string
}
type Result struct {
fetching *Fetching
seller string
price float64
buybox bool
}
type MonitorStore struct {
conn *sqlite.Conn
}
func NewMonitorStore(filename string) *MonitorStore {
store := &MonitorStore{}
conn, err := sqlite.Open(filename)
if err != nil {
fmt.Println("Unable to open database: %s", err)
os.Exit(1);
}
//create table
conn.Exec("CREATE TABLE IF NOT EXISTS asin(id INTEGER PRIMARY KEY AUTOINCREMENT, asin VARCHAR(200));")
store.conn = conn
return store
}
func (s *MonitorStore) AddAsin(asin string) {
insertSql := `insert into asin (asin) values ("` + asin + `");`
s.conn.Exec(insertSql)
}
func (s *MonitorStore) GetAsinList() []string {
list := make([]string, 20)
stmt, err := s.conn.Prepare("select * from Asin limit 10;")
err = stmt.Exec()
if err != nil {
fmt.Println("Error While Selecting: %s", err)
}
for stmt.Next() {
var asin Asin
err = stmt.Scan(&asin.id, &asin.asin)
if err != nil {
fmt.Printf("Error while getting row data:%s\n", err)
os.Exit(1)
}
fmt.Println(asin.id, asin.asin)
list[asin.id] = asin.asin
}
return list
}