# error: sql: Rows are closed

- **ID:** `go/database-sql-rows-iteration-error-after-close`
- **Domain:** go
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Attempting to iterate over database/sql Rows after they have been closed, typically due to premature defer row.Close() or double iteration.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.20 | active | — | — |
| 1.21 | active | — | — |

## Workarounds

1. **Defer rows.Close() after checking error and before iteration, but ensure iteration completes before close** (95% success)
   ```
   rows, err := db.QueryContext(ctx, query)
if err != nil { return err }
defer rows.Close()
for rows.Next() { /* process */ }
if err = rows.Err(); err != nil { return err }
   ```

## Dead Ends

- **Calling rows.Close() at beginning of function** — Deferring Close before iteration closes rows prematurely, causing 'Rows are closed' error on Next(). (90% fail)
