Server-Side Scripting/SQL Databases/Go

routes/lesson9.go
"   rows.Close    return result; }

func countryExists(country string) bool { query := ` SELECT EXISTS(       SELECT * FROM Countries        WHERE Country = ?) AS Count;`

rows := sqliteQuery(query, country) var count int rows.Next rows.Scan(&count) rows.Close return count == 1 }

func insertCountry(country string, temperature string) { query := ` INSERT INTO Countries (Country, Temperature) VALUES(?, ?);` sqliteExec(query, country, temperature); }

func updateCountry(country string, temperature string) { query := ` UPDATE Countries SET Temperature = ? WHERE Country = ?;` sqliteExec(query, temperature, country); }

func deleteCountry(country string) { query := ` DELETE FROM Countries WHERE Country = ?;` sqliteExec(query, country); }

func sqliteQuery(query string, parameters ...interface{}) *sql.Rows { db, err := sql.Open("sqlite3", "./temperature.db") if err != nil { log.Fatal(err) }   defer db.Close

var result *sql.Rows if parameters == nil { result, err = db.Query(query) } else { result, err = db.Query(query, parameters...) }   if err != nil { log.Fatal(err) }

return result }

func sqliteExec(query string, parameters ...interface{}) sql.Result { db, err := sql.Open("sqlite3", "./temperature.db") if err != nil { log.Fatal(err) }   defer db.Close

var result sql.Result if parameters == nil { result, err = db.Exec(query) } else { result, err = db.Exec(query, parameters...) }   if err != nil { log.Fatal(err) }

return result }

Try It
See ../../Routes and Templates/Go to create a test environment.