0PricingLogin
Scala for Backend Engineering & Functional Programming · Lesson

Running Queries

Read rows into case classes.

The sql Interpolator

Doobie's sql string interpolator is the core of querying. It parses your SQL and turns embedded Scala values into safe, bound parameters.

Interpolated values become JDBC ? placeholders, so there is no SQL injection and no manual PreparedStatement wiring.

import doobie.implicits._

val minAge = 18
val frag = sql"select name from users where age >= $minAge"

From Fragment to Query

An sql"..." expression is a Fragment. To run it as a read you call .query[A], choosing the row type A.

Doobie uses a Read[A] typeclass to map result columns onto A, supporting primitives, tuples, and case classes.

case class User(name: String, age: Int)

val q: Query0[User] =
  sql"select name, age from users".query[User]

All lessons in this course

  1. Connecting with a Transactor
  2. Running Queries
  3. Inserts and Updates
  4. Composing Transactions
← Back to Scala for Backend Engineering & Functional Programming