SQLITE_MISMATCH: datatype mismatch
This error occurs when you try to store a value that doesn't match the expected type, typically with PRIMARY KEY columns. Learn about SQLite's type affinity system.
The datatype mismatch error usually occurs with PRIMARY KEY constraints when the value type doesn't match expectations.
Understanding the Error
SQLITE_MISMATCH: datatype mismatch
This commonly happens with INTEGER PRIMARY KEY columns when you try to insert a non-integer value.
Common Causes
1. Non-Integer in INTEGER PRIMARY KEY
SQL
2. ROWID Alias Mismatch
INTEGER PRIMARY KEY is an alias for SQLite's internal ROWID:
SQL
3. Strict Tables (SQLite 3.37+)
Strict mode enforces types:
SQL
How to Fix It
Solution 1: Use Correct Types
SQL
Solution 2: Use TEXT Primary Key
If you need string IDs:
SQL
Solution 3: Convert Types in Application
JAVASCRIPT
SQLite Type Affinity
SQLite is generally flexible with types, but INTEGER PRIMARY KEY is special:
SQL
Best Practices
- Use NULL for auto-increment IDs instead of omitting the column
- Validate types in application code
- Consider TEXT keys for UUIDs or string identifiers
- Test with edge cases (strings, floats, nulls)