> ## Documentation Index
> Fetch the complete documentation index at: https://docs.encodebox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Examples

> Runnable EncodeBox examples against the sample Sentinel CDM database.

Runnable patterns for FDA Sentinel CDM with the sample database and the unified query API.

## Prerequisites

```bash theme={null}
cd /path/to/encodebox
pixi install -e dev
pixi run db-up
```

Run examples:

```bash theme={null}
python examples/simple_example.py
```

## Three key steps

<Steps>
  <Step title="Register connector">
    ```python theme={null}
    import encodebox as eb

    eb.register_connector("connectors/postgres_local_sentinel/connector.yml")
    ```
  </Step>

  <Step title="Connect → Database">
    ```python theme={null}
    db = eb.connect(
        "postgres",
        host="localhost",
        port=5433,
        database="encodebox_sample",
        username="postgres",
        password="postgres",
        connector="postgres_local_sentinel",
    )
    ```
  </Step>

  <Step title="Query">
    ```python theme={null}
    diabetes = (
        db.diagnosis
        .matching("E11", code_type="10", match="starts_with")
    )
    df = diabetes.to_df()
    ```
  </Step>
</Steps>

## Common queries

```python theme={null}
# Diagnoses
db.diagnosis.matching("E11", code_type="10", match="starts_with")

# Procedures
db.procedure.matching("45378", code_type="C4", match="exactly")

# Medications (dispensing)
db.medication.matching("00002-7510", code_type="ND", match="starts_with")

# Multiple code sets (UNION → code_set)
db.diagnosis.matching(
    [["E11"], ["I10"]],
    code_type="10",
    match=["starts_with", "exactly"],
)
```

## Occurrence and gap

```python theme={null}
index = (
    db.diagnosis
    .matching("E11", code_type="10", match="starts_with")
    .occurring(at_least=2, gap_days=30)
    .with_index_date()
    .first_per_patient()
)
```

## Cohort study

```python theme={null}
cohort = (
    eb.Cohort("t2dm", database=db)
    .entry(index, index="first")
    .include(db.demographic.age_at_index(18, 89))
    .include(db.enrollment.covering_index(days_before=365, days_after=0))
    .exclude(db.diagnosis.matching("C", match="starts_with"), window=(-365, 0))
)
print(cohort.attrition())
df = cohort.to_df()
```

See also: [Walkthrough](/walkthrough), [Query patterns](/query-patterns).
