-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
75 lines (67 loc) · 2.36 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
71
72
73
74
75
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"log"
"strconv"
"strings"
"unicode"
)
// initDB creates DB if it doesn't exit
func initDB() {
database, err := sql.Open("sqlite3", "./wifidata.db")
if err != nil {
log.Printf("Error initilizing Database: %v", err)
}
wifidata, err := database.Prepare("CREATE TABLE IF NOT EXISTS wifidata (id INTEGER PRIMARY KEY, essid TEXT, mac TEXT, freq TEXT, siglvl TEXT, " +
"qual TEXT, enc TEXT, channel INT, mode TEXT, ieee TEXT, bitrates TEXT, wpa TEXT, tmstmp TEXT, latitude TEXT, longitude TEXT, read BOOL, UNIQUE(essid, mac))")
if err != nil {
log.Printf("Error creating table: %v", err)
}
wifidata.Exec()
}
// writeWiFiDB writes all data from scan to DB
func writeWiFiDB(data []wifiData, timestamp int64) {
database, err := sql.Open("sqlite3", "./wifidata.db")
if err != nil {
log.Printf("Can't open database: %v", err)
}
defer database.Close()
for _, item := range data {
if item.MAC != "" {
statement, _ := database.Prepare("INSERT OR IGNORE INTO wifidata (essid, mac, freq, siglvl, qual, enc, channel, mode, ieee, bitrates, wpa, tmstmp, latitude, longitude, read) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
tm := strconv.FormatInt(timestamp, 10)
statement.Exec(stripSpaces(item.ESSID), stripSpaces(item.MAC), item.Freq, item.SigLvl, item.Qual, item.Enc, item.Channel, item.Mode, item.IEEE, item.Bitrates, item.WPA, tm, GPSdata.Latitude, GPSdata.Longitute, GPSdata.GPSRead)
}
}
}
func readDB() *[]webdata {
database, err := sql.Open("sqlite3", "./wifidata.db")
if err != nil {
log.Printf("Can't open database: %v", err)
}
defer database.Close()
rows, _ := database.Query("SELECT tmstmp, essid, mac, freq, siglvl, qual, enc, channel, mode, ieee, bitrates, wpa, " +
" latitude, longitude, read FROM wifidata")
var item webdata
var retdata []webdata
{
}
for rows.Next() {
rows.Scan(&item.Timestamp, &item.ESSID, &item.MAC, &item.Freq, &item.SigLvl, &item.Qual, &item.Enc,
&item.Channel, &item.Mode, &item.IEEE, &item.Bitrates, &item.WPA, &item.Latitude, &item.Longitude, &item.GPSRead)
retdata = append(retdata, item)
}
return &retdata
}
func stripSpaces(str string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
// if the character is a space, drop it
return -1
}
// else keep it in the string
return r
}, str)
}