# android.database.sqlite.SQLiteException: 无法从游标读取: 列'timestamp'类型为TEXT但期望INTEGER（代码0）

- **ID:** `android/room-migration-ambiguous-column-type`
- **领域:** android
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 87%

## 根因

Room迁移脚本更改了架构但未转换现有数据类型，导致预期SQLite亲和类型（INTEGER）与列中实际存储类型（TEXT）不匹配。

## 版本兼容性

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

## 解决方案

1. ```
   Write a manual migration that converts TEXT to INTEGER using CAST. Example in migration: ALTER TABLE my_table ADD COLUMN timestamp_temp INTEGER; UPDATE my_table SET timestamp_temp = CAST(timestamp AS INTEGER); ALTER TABLE my_table DROP COLUMN timestamp; ALTER TABLE my_table RENAME COLUMN timestamp_temp TO timestamp;
   ```
2. ```
   Use @TypeConverter to handle both types in the DAO. Example: @TypeConverter public static Long fromTimestamp(String value) { return value == null ? null : Long.parseLong(value); }
   ```

## 无效尝试

- **Delete app data and reinstall** — This only works for development but destroys user data in production. The underlying migration issue is not fixed. (80% 失败率)
- **Add fallbackToDestructiveMigration() to Room database builder** — This drops the entire database on migration failure, losing all user data. It's a nuclear option that doesn't fix the type mismatch. (90% 失败率)
