From b270a45ece94b184eabf5632977c997a83600844 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Sun, 23 Jan 2022 23:26:46 +0000 Subject: [PATCH] Cleaned up Application - Inserts every Itteration --- main.go | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 6084a59..44b98d1 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "os" + "time" _ "github.com/go-sql-driver/mysql" ) @@ -15,23 +16,32 @@ func main() { jsonString, _ := os.ReadFile("./waypoints.json") data := []Waypoint{} - x := json.Unmarshal(jsonString, &data) - - fmt.Println(string(jsonString), x) + json.Unmarshal(jsonString, &data) //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 { fmt.Println(err) } 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')") - //Close the connection - if err != nil { - fmt.Println(err) - } - if insert != nil { - fmt.Println("Success") + + 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 + if err != nil { + fmt.Println(err) + } + insert.Close() } + + fmt.Println("Total Execution Time:", time.Since(currentTime)) }