Connecting to an in-memory DB

Report a typo

When working with sqlite3 databases, it is possible to create and connect to an in-memory database named :memory:.

Suppose you wanted to connect to the in-memory sqlite3 database :memory: using the functions from the database/sql package. Your task is to write the required additional code to connect to :memory: using the sql.Open() function.

Write a program in Go
package main

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

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

func main() {
// Write the required code to connect to the sqlite3 ":memory:" database below:
db, err := ?.?("?", ":memory:")
must(err)
defer db.Close()

// DO NOT delete the code block below; it checks if a proper connection was made to ":memory:"
err = db.Ping() // Ping the database to check the connection
must(err)
fmt.Println(checkConnection(db)) // Check if the connection was successful or not
}

// DO NOT delete the must() function — it is a helper to check for errors!
func must(err error) {
if err != nil {
log.Fatal(err)
}
}
___

Create a free account to access the full topic