Write a SELECT query

Report a typo

Ivan has created a Go program that creates an in-memory sqlite3 database with the continents table via the hidden populateDB() function. However, he doesn't know how to retrieve data from the continents table using Go!

First, you must help Ivan use the db.Query() function to write a SELECT query to select the continent_id and name columns from the continents table.

Next, you should use a for loop with the rows.Next() function to iterate over the selected rows, then use the rows.Scan() function to read the data into the continentId and name variables, and finally print the data of each row.

Write a program in Go
package main

import (
"database/sql"
"fmt"
"log"

_ "github.com/mattn/go-sqlite3"
)

func main() {
// DO NOT delete the code block below!
// it connects to an in-memory sqlite3 DB and then populates 'continents' with data!
db, err := sql.Open("sqlite3", ":memory:")
must(err)
defer db.Close()
populateDB(db) // DO NOT delete this line; it populates the 'continents' table!

// Write the 'SELECT' query and select the 'continent_id' and 'name' columns below:
rows, err := db.Query("SELECT ?, ? FROM ?")
must(err)
defer rows.Close()

// Use the rows.Next() function to iterate over the selected rows:
var continentID, name string
for ?() {
// Read the data into the 'continentID' and 'name' variables:
err = rows.Scan(&?, &?)
must(err)
// Output the selected data contained in the 'continentID' and 'name' variables:
fmt.Println(?, ?)
}

// DO NOT delete the code block below; it checks for any errors:
if err = rows.Err(); err != nil {
panic(err)
___

Create a free account to access the full topic