-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddtodb.go
71 lines (65 loc) · 1.4 KB
/
addtodb.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
package main
import (
"database/sql/driver"
sq "github.com/mattn/go-sqlite3"
"strconv"
)
func createDB(dbname string) (driver.Conn, error) {
d, e := new(sq.SQLiteDriver).Open(dbname)
if e != nil {
return nil, e
}
return d, nil
}
func addBus(b *busdet, table string, dbcon driver.Conn) error {
jmin := strconv.Itoa(b.jmin)
insertstr := "INSERT INTO " + table + " VALUES(" +
strconv.Quote(b.routenum) + "," +
strconv.Quote(b.servtype) + "," +
strconv.Quote(b.origin) + "," +
strconv.Quote(b.dest) + "," + jmin + ");"
e := commitTrans(dbcon, insertstr)
if e != nil {
return e
}
return nil
}
func addBusPath(b *busdet, c driver.Conn) error {
colmns := "id INTEGER, stagename TEXT"
tablename := "p" + b.routenum
e := createTable(c, tablename, colmns)
if e != nil {
return e
}
for i, p := range b.stname {
pq := "INSERT INTO " + tablename + " VALUES(" + strconv.Itoa(i+1) + "," + strconv.Quote(p) + ");"
e = commitTrans(c, pq)
if e != nil {
return e
}
}
return nil
}
func createTable(c driver.Conn, t, colmns string) error {
tq := "CREATE TABLE IF NOT EXISTS " + t + "(" + colmns + ");"
e := commitTrans(c, tq)
if e != nil {
return e
}
return nil
}
func commitTrans(dbcon driver.Conn, cmd string) error {
dbstmt, e := dbcon.Prepare(cmd)
if e != nil {
return e
}
_, e = dbstmt.Exec(nil)
if e != nil {
return e
}
e = dbstmt.Close()
if e != nil {
return e
}
return nil
}