# sqlite3.OperationalError: unable to open database: /path/to/attached.db

- **ID:** `database/sqlite-cannot-attach-database`
- **Domain:** database
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

The ATTACH DATABASE command fails because the target database file does not exist, is not readable, or the directory is not writable (for creating a new database), or the file is locked by another process.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| SQLite 3.36 | active | — | — |
| SQLite 3.40 | active | — | — |
| SQLite 3.45 | active | — | — |
| SQLite 3.46 | active | — | — |

## Workarounds

1. **Check if the file exists: `ls -la /path/to/attached.db`. If not, create an empty database file: `touch /path/to/attached.db` and ensure correct permissions: `chmod 644 /path/to/attached.db`. Then retry the ATTACH command: `ATTACH DATABASE '/path/to/attached.db' AS attached_db;`** (85% success)
   ```
   Check if the file exists: `ls -la /path/to/attached.db`. If not, create an empty database file: `touch /path/to/attached.db` and ensure correct permissions: `chmod 644 /path/to/attached.db`. Then retry the ATTACH command: `ATTACH DATABASE '/path/to/attached.db' AS attached_db;`
   ```
2. **Use a tool like `lsof /path/to/attached.db` to check if another process has the file locked. If so, kill or wait for the process to release the lock, or use a different database file path.** (80% success)
   ```
   Use a tool like `lsof /path/to/attached.db` to check if another process has the file locked. If so, kill or wait for the process to release the lock, or use a different database file path.
   ```
3. **Ensure the parent directory is writable: `chmod 755 /path/to` and that the file is not in a read-only filesystem. If using NFS, check for NFS lock issues and consider using a local file path.** (75% success)
   ```
   Ensure the parent directory is writable: `chmod 755 /path/to` and that the file is not in a read-only filesystem. If using NFS, check for NFS lock issues and consider using a local file path.
   ```

## Dead Ends

- **Set the file permissions to 777 recursively on the directory** — Overly permissive permissions can cause security issues and may not solve the problem if the file is locked by another process (50% fail)
- **Use ATTACH DATABASE with a different alias expecting it to create the file** — ATTACH DATABASE can create a new database file only if the directory is writable; if the error is due to a locked file, a new alias won't help (40% fail)
- **Restart the application to release locks** — If the lock is held by another process (e.g., a separate application instance), restarting your app won't release that lock (60% fail)
