Cleaned up Application - Inserts every Itteration

This commit is contained in:
Luke Else 2022-01-23 23:26:46 +00:00
parent d4d892f6f2
commit b270a45ece

26
main.go
View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"time"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
@ -15,23 +16,32 @@ func main() {
jsonString, _ := os.ReadFile("./waypoints.json") jsonString, _ := os.ReadFile("./waypoints.json")
data := []Waypoint{} data := []Waypoint{}
x := json.Unmarshal(jsonString, &data) json.Unmarshal(jsonString, &data)
fmt.Println(string(jsonString), x)
//Open a connection to the database //Open a connection to the database
db, err := sql.Open("mysql", "root:@tcp(:3306)/EFB") db, err := sql.Open("mysql", "root:XXXX@tcp(XXX.XXX.XXX.XXX:3306)/EFB")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
defer db.Close() defer db.Close()
//Make a Query to the database
insert, err := db.Query("INSERT INTO waypoints (name, type, frequency, longitude, latitude) VALUES ('WILLO', 'NDB', 12000, 'test', 'test')") currentTime := time.Now()
//Make a Query to the database for each item
for _, waypoint := range data {
insert, err := db.Query("INSERT INTO waypoints (name, type, latitude, longitude) VALUES (?, ?, ?, ?)",
waypoint.Name,
waypoint.NavType,
waypoint.Coords[0],
waypoint.Coords[1],
)
//Close the connection //Close the connection
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
if insert != nil { insert.Close()
fmt.Println("Success")
} }
fmt.Println("Total Execution Time:", time.Since(currentTime))
} }