android data_error ai_generated true

android.database.sqlite.SQLiteException:无效的列类型(代码 0):无法将列 'timestamp' 读取为 long 类型(代码 0),位于 /path/to/RoomDatabase.kt:123

android.database.sqlite.SQLiteException: Invalid column type (code 0): Cannot read column 'timestamp' as a long (code 0) at /path/to/RoomDatabase.kt:123

ID: android/room-invalid-cursor-query

其他格式: JSON · Markdown 中文 · English
88%修复率
87%置信度
1证据数
2023-11-05首次发现

版本兼容性

版本状态引入弃用备注
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

根因分析

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

English

Room's generated DAO implementation tries to read a column with a type that does not match the expected Java/Kotlin type, often due to a schema mismatch or incorrect converter.

generic

官方文档

https://developer.android.com/training/data-storage/room

解决方案

  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.

无效尝试

常见但无效的做法:

  1. Change the field type in the entity to match the error message, e.g., change long to String 85% 失败

    The error indicates an actual schema mismatch; changing the type without updating the database version and migration leads to a crash or data loss.

  2. Delete the app data and reinstall without running migrations 80% 失败

    This destroys user data and is not suitable for production; it only works if the schema was never properly migrated.

  3. Add @Ignore annotation to the problematic field 90% 失败

    Room will ignore the field entirely, potentially breaking queries that depend on it.