# android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: users.email (code 1555)

- **ID:** `android/sqliteconstraintexception-unique-constraint`
- **Domain:** android
- **Category:** data_error
- **Error Code:** `1555`
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

An INSERT or UPDATE operation attempted to set a value on a column with a UNIQUE constraint that already exists in the table.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| SQLite 3.42 | active | — | — |
| Android 12 (API 31) | active | — | — |
| Android 13 (API 33) | active | — | — |
| Room 2.5.2 | active | — | — |

## Workarounds

1. **Use INSERT OR REPLACE or INSERT OR IGNORE in your SQL statement. Example: ContentValues values = new ContentValues(); values.put("email", "test@example.com"); db.insertWithOnConflict("users", null, values, SQLiteDatabase.CONFLICT_REPLACE);** (90% success)
   ```
   Use INSERT OR REPLACE or INSERT OR IGNORE in your SQL statement. Example: ContentValues values = new ContentValues(); values.put("email", "test@example.com"); db.insertWithOnConflict("users", null, values, SQLiteDatabase.CONFLICT_REPLACE);
   ```
2. **Check for existing data before inserting: if (db.rawQuery("SELECT 1 FROM users WHERE email = ?", new String[]{email}).getCount() == 0) { db.insert(...); }** (85% success)
   ```
   Check for existing data before inserting: if (db.rawQuery("SELECT 1 FROM users WHERE email = ?", new String[]{email}).getCount() == 0) { db.insert(...); }
   ```
3. **Use Room's @Insert(onConflict = OnConflictStrategy.REPLACE) annotation to handle duplicate entries automatically.** (95% success)
   ```
   Use Room's @Insert(onConflict = OnConflictStrategy.REPLACE) annotation to handle duplicate entries automatically.
   ```

## Dead Ends

- **** — Clearing app cache does not affect the database, so the error persists. (95% fail)
- **** — Adding a try-catch around the INSERT without handling the duplicate (e.g., ignoring the error) may cause data loss or silent failures. (75% fail)
- **** — Disabling the unique constraint via ALTER TABLE may break data integrity and cause other bugs. (85% fail)
