SQLiteException code 0
android
data_error
ai_generated
true
android.database.sqlite.SQLiteException: Cannot read from cursor: column 'price' has type REAL but expected INTEGER (code 0)
ID: android/sqlite-cursor-column-type-mismatch
92%Fix Rate
88%Confidence
1Evidence
2023-11-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Android 11 (API 30) | active | — | — | — |
| Android 12 (API 31) | active | — | — | — |
| Android 13 (API 33) | active | — | — | — |
Root Cause
Cursor attempts to read a REAL column as INTEGER via getInt(), causing type mismatch in SQLite's dynamic typing system.
generic中文
游标尝试通过 getInt() 将 REAL 列读取为 INTEGER,导致 SQLite 动态类型系统中的类型不匹配。
Official Documentation
https://developer.android.com/reference/android/database/sqlite/SQLiteExceptionWorkarounds
-
95% success Use `cursor.getDouble(cursor.getColumnIndexOrThrow("price"))` instead of getInt() to read REAL columns correctly.
Use `cursor.getDouble(cursor.getColumnIndexOrThrow("price"))` instead of getInt() to read REAL columns correctly. -
92% success In Room, annotate the field as `Double` (not Int) in the entity class: `@ColumnInfo(name = "price") val price: Double`
In Room, annotate the field as `Double` (not Int) in the entity class: `@ColumnInfo(name = "price") val price: Double`
-
85% success Use `cursor.getType(columnIndex)` to check the type before reading: if type == Cursor.FIELD_TYPE_FLOAT, use getDouble(); else getInt().
Use `cursor.getType(columnIndex)` to check the type before reading: if type == Cursor.FIELD_TYPE_FLOAT, use getDouble(); else getInt().
中文步骤
Use `cursor.getDouble(cursor.getColumnIndexOrThrow("price"))` instead of getInt() to read REAL columns correctly.In Room, annotate the field as `Double` (not Int) in the entity class: `@ColumnInfo(name = "price") val price: Double`
Use `cursor.getType(columnIndex)` to check the type before reading: if type == Cursor.FIELD_TYPE_FLOAT, use getDouble(); else getInt().
Dead Ends
Common approaches that don't work:
-
Cast the column value to int in SQL query: `CAST(price AS INTEGER)`
70% fail
This truncates decimals, losing precision; the real fix is to read as getDouble().
-
Change column type in CREATE TABLE to INTEGER
85% fail
SQLite is dynamically typed; the column type only affects affinity, not actual stored values. Existing data remains REAL.
-
Use getString() and parse to int
90% fail
Parsing '12.99' to int throws NumberFormatException or truncates incorrectly.