Inserts and Updates
Write data functionally.
Update0 for Writes
Writes use the same sql interpolator, but you call .update to get an Update0 instead of .query.
Running .run on an Update0 returns a ConnectionIO[Int]: the number of affected rows.
import doobie.implicits._
def insert(name: String, age: Int): ConnectionIO[Int] =
sql"insert into users (name, age) values ($name, $age)".update.runAffected Row Counts
The Int from .run is the JDBC update count. For an UPDATE or DELETE it tells you how many rows matched.
You can branch on it to detect, for example, an update that matched no rows.
def deactivate(id: Long): ConnectionIO[Int] =
sql"update users set active = false where id = $id".update.runAll lessons in this course
- Connecting with a Transactor
- Running Queries
- Inserts and Updates
- Composing Transactions