# android.database.sqlite.SQLiteException：无效的列类型（代码 0）：无法将列 'timestamp' 读取为 long 类型（代码 0），位于 /path/to/RoomDatabase.kt:123

- **ID:** `android/room-invalid-cursor-query`
- **领域:** android
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

Room 生成的 DAO 实现尝试读取与预期 Java/Kotlin 类型不匹配的列，通常是由于模式不匹配或转换器错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Room 2.4.0 | active | — | — |
| Room 2.5.0 | active | — | — |
| Room 2.6.0 | active | — | — |
| Room 2.7.0 | active | — | — |
| Android 8.0 | active | — | — |
| Android 9 | active | — | — |
| Android 10 | active | — | — |
| Android 11 | active | — | — |
| Android 12 | active | — | — |
| Android 13 | active | — | — |
| Android 14 | active | — | — |
| Android 15 | active | — | — |

## 解决方案

1. ```
   Check the entity and database version. If the column type changed, create a migration. Example: static final Migration MIGRATION_1_2 = new Migration(1, 2) { @Override public void migrate(@NonNull SupportSQLiteDatabase database) { database.execSQL("ALTER TABLE users ADD COLUMN timestamp INTEGER NOT NULL DEFAULT 0"); } }; Then add .addMigrations(MIGRATION_1_2) to the Room database builder.
   ```
2. ```
   If the column type is correct but the converter is wrong, verify the TypeConverter. Example: @TypeConverter public static Long fromTimestamp(Long value) { return value == null ? null : value; } @TypeConverter public static Long dateToTimestamp(Long value) { return value; } Ensure both converters are symmetric and registered in @Database(entities = {...}, version = X, exportSchema = true).
   ```
3. ```
   Use fallbackToDestructiveMigration() in the database builder for development: Room.databaseBuilder(context, AppDatabase.class, "db-name").fallbackToDestructiveMigration().build(); This recreates the database if no migration is found, but destroys data.
   ```

## 无效尝试

- **Change the field type in the entity to match the error message, e.g., change long to String** — The error indicates an actual schema mismatch; changing the type without updating the database version and migration leads to a crash or data loss. (85% 失败率)
- **Delete the app data and reinstall without running migrations** — This destroys user data and is not suitable for production; it only works if the schema was never properly migrated. (80% 失败率)
- **Add @Ignore annotation to the problematic field** — Room will ignore the field entirely, potentially breaking queries that depend on it. (90% 失败率)
