Module 08 Β· Databases with database/sql β Lesson 2 of 3 Β· ~9 min
Writing & multiple rows
Changing data: Exec
QueryRow and Query are for reads. When a statement changes the database β INSERT, UPDATE, DELETE β you use db.Exec. It doesn't return rows; it returns a sql.Result with metadata like how many rows changed.
res, err := db.Exec("INSERT INTO walkers (name) VALUES ($1)", name)
Same placeholder rule as before: the new name goes in as a $1 argument, never concatenated. If you don't care about the result metadata, you can ignore it with _, err := db.Exec(...).