Skip to content

Row Querying

db.query(sql) executes a SELECT and returns Result<[Any], DbError>. Unwrap or handle the error; on success the value is a list of maps where each key is a column name. You iterate with for row in rows and access fields like row.name, row.qty — no manual mapping required.

The list supports rows.len() and all of Zolo's collection methods (.each, .filter, .map, etc.).

Selects items ordered by quantity and prints each row using direct field access on the result map.

03-query-rows.zolo
// Feature: Database — `db.query(sql)` returns a list of rows

// Syntax: each row is a `{col: value}` map; iterate with `for row in rows`.

// When to use: SELECT in general; Zolo serializes the result into maps.


use std::Database

let db = Database.open("sqlite://:memory:").unwrap()
defer db.close()

db.execute(sql"CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT, qty INTEGER)").unwrap()
db.execute(sql"INSERT INTO items VALUES (1, 'apple', 10)").unwrap()
db.execute(sql"INSERT INTO items VALUES (2, 'banana', 5)").unwrap()
db.execute(sql"INSERT INTO items VALUES (3, 'cherry', 20)").unwrap()

let rows = db.query(sql"SELECT * FROM items ORDER BY qty DESC").unwrap()
print("total: {rows.len()}")

// expected: total: 3


for row in rows {
  print("  {row.id}: {row.name} x{row.qty}")
}
// expected:

//   3: cherry x20

//   1: apple x10

//   2: banana x5

Requires the Zolo CLI/host — open in the playground or run locally.

Challenge

Add a .filter to the rows list to display only items with qty > 8 without changing the SQL.

enespt-br