Solution: Databases
Explore how to persist data from a Pomodoro application into an SQLite database using Go. Understand setting up temporary databases, preparing SQL insert statements, inserting timed Pomodoro and break records, and managing errors to ensure reliable data storage.
We'll cover the following...
We'll cover the following...
Solution explanation
In the pomodoro_test.go file in the setup function, we start by creating a temporary database file:
t.Helper()
tf, err := ioutil.TempFile("", "pomo")
if err != nil {
t.Fatal(err)
}
tf.Close()
We then open a DB connection to the SQLite database. If the connection cannot be made due to some error, the relevant message is thrown and the program is ended:
dbRepo, err := repository.NewSQLite3Repo(tf.Name())
if err != nil ...