-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexport-all.go
56 lines (52 loc) · 1.79 KB
/
export-all.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
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"database/sql"
"os"
//"encoding/csv"
"strings"
)
func export_all(dbtype string, constring string) {
var line []string
//var line2 string
// set up the CSV file //
//file, err := os.Create("export.csv")
f2, err := os.Create("export_raw.csv")
//checkError("Cannot create file", err)
//defer file.Close()
defer f2.Close()
// set up the handle for writing //
//writer := csv.NewWriter(file)
//defer writer.Flush()
// prep and connect to database //
db, err := sql.Open(dbtype, constring)
defer db.Close()
//results, err := db.Query("SELECT * FROM hosts")
results, err := db.Query("SELECT INET_NTOA(ipv4_address), HEX(dhcp_identifier), dhcp4_subnet_id, hostname FROM hosts")
fmt.Println("Dumping all reservations.")
for results.Next() {
rows++
var tag kea_entry
err = results.Scan(&tag.ipntoa, &tag.dehexmac, &tag.dhcp_id, &tag.hname)
if err != nil {
panic(err.Error())
}
fmt.Println("----------------------------------------------------------------")
fmt.Println("|",tag.ipntoa,"|",tag.dehexmac,"|", tag.hname,"|",tag.dhcp_id,"|")
fmt.Println("----------------------------------------------------------------")
line = []string{tag.ipntoa,tag.dehexmac,tag.hname,tag.dhcp_id}
linestr := strings.Join(line,",")
//writer.Write(line)
f2.WriteString(linestr)
f2.WriteString("\n")
//fmt.Println(linestr)
}
if (debug) {
fmt.Println("Records returned: ", rows)
}
db.Close()
//file.Close()
f2.Close()
f2.Close()
}