My main function opens a database connection:
func main() {
db, err := sql.Open("sqlite3", "./house.db")
checkErr(err)
...
}
Then, I want to create a function that allows me to add a row to the database based on a passed struct:
func addRow(row Room) error {
stmt, err := db.Prepare("INSERT INTO Rooms (Name, Size, WindowCount, WallDecorationType, Floor) VALUES(?, ?, ?, ?, ?)")
_, err = stmt.Exec(row.Name , row.Size , row.WindowCount , row.WallDecorationType , row.Floor)
return err
}
But obviously I can't do that because the addRow()
function has no idea what db
is.
How would I make this function work? Should I perhaps, open the database outside of the main function?