# android.database.sqlite.SQLiteException：无法从游标读取：列 'price' 的类型是 REAL，但期望的是 INTEGER（代码 0）

- **ID:** `android/sqlite-cursor-column-type-mismatch`
- **领域:** android
- **类别:** data_error
- **错误码:** `SQLiteException code 0`
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

游标尝试通过 getInt() 将 REAL 列读取为 INTEGER，导致 SQLite 动态类型系统中的类型不匹配。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Android 11 (API 30) | active | — | — |
| Android 12 (API 31) | active | — | — |
| Android 13 (API 33) | active | — | — |

## 解决方案

1. ```
   Use `cursor.getDouble(cursor.getColumnIndexOrThrow("price"))` instead of getInt() to read REAL columns correctly.
   ```
2. ```
   In Room, annotate the field as `Double` (not Int) in the entity class: `@ColumnInfo(name = "price") val price: Double`
   ```
3. ```
   Use `cursor.getType(columnIndex)` to check the type before reading: if type == Cursor.FIELD_TYPE_FLOAT, use getDouble(); else getInt().
   ```

## 无效尝试

- **Cast the column value to int in SQL query: `CAST(price AS INTEGER)`** — This truncates decimals, losing precision; the real fix is to read as getDouble(). (70% 失败率)
- **Change column type in CREATE TABLE to INTEGER** — SQLite is dynamically typed; the column type only affects affinity, not actual stored values. Existing data remains REAL. (85% 失败率)
- **Use getString() and parse to int** — Parsing '12.99' to int throws NumberFormatException or truncates incorrectly. (90% 失败率)
