-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
41 lines (35 loc) · 875 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
package db
import (
"database/sql"
"log"
"sync"
_ "github.com/mattn/go-sqlite3"
)
type Database struct {
Db *sql.DB
}
var instance *Database
var once sync.Once
// Connect is the "constructor" for a database. It wraps a singleton Database object so that only one connection is opened at a time
func Connect() *Database {
once.Do(func() {
// create and connect to db
database, err := sql.Open("sqlite3", "./sniffed.db")
if err != nil {
log.Fatalf("Error connecting to database: %v", err)
}
instance = &Database{Db: database}
_, err = instance.Db.Exec(`CREATE TABLE networks (
uid INTEGER PRIMARY KEY AUTOINCREMENT,
mac TEXT NOT NULL,
power TEXT NOT NULL,
packetCount TEXT NOT NULL,
bssid TEXT NOT NULL,
essid TEXT NOT NULL
);`)
if err != nil {
log.Printf("Failed to initialize database: %v", err)
}
})
return instance
}