1 android data_error ai_generated true

android.database.sqlite.SQLiteException: no such table: users (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM users

ID: android/room-cursor-not-found

Also available as: JSON · Markdown · 中文
85%Fix Rate
85%Confidence
1Evidence
2023-03-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Room 2.5.0 active
Room 2.6.0 active

Root Cause

Room database migration failed or the schema version is mismatched, causing the table to not exist in the current database.

generic

中文

Room 数据库迁移失败或架构版本不匹配,导致当前数据库中不存在该表。

Official Documentation

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

Workarounds

  1. 85% success Add a proper migration to the Room database builder. Example: Room.databaseBuilder(context, AppDatabase.class, "my-db").addMigrations(MIGRATION_1_2).build(); where MIGRATION_1_2 is a Migration object that creates the table.
    Add a proper migration to the Room database builder. Example: Room.databaseBuilder(context, AppDatabase.class, "my-db").addMigrations(MIGRATION_1_2).build(); where MIGRATION_1_2 is a Migration object that creates the table.
  2. 95% success Use fallbackToDestructiveMigration() to recreate the database, but this will delete existing data. Only use in development or if data loss is acceptable.
    Use fallbackToDestructiveMigration() to recreate the database, but this will delete existing data. Only use in development or if data loss is acceptable.
  3. 80% success Export the schema and verify the JSON file matches the entities. Add @Database(entities = {User.class}, version = 2, exportSchema = true) and check the generated schema in app/schemas/.
    Export the schema and verify the JSON file matches the entities. Add @Database(entities = {User.class}, version = 2, exportSchema = true) and check the generated schema in app/schemas/.

中文步骤

  1. 向 Room 数据库构建器添加正确的迁移。示例:Room.databaseBuilder(context, AppDatabase.class, "my-db").addMigrations(MIGRATION_1_2).build(); 其中 MIGRATION_1_2 是创建表的 Migration 对象。
  2. 使用 fallbackToDestructiveMigration() 重新创建数据库,但这会删除现有数据。仅在开发中或可接受数据丢失时使用。
  3. 导出架构并验证 JSON 文件与实体匹配。添加 @Database(entities = {User.class}, version = 2, exportSchema = true) 并检查 app/schemas/ 中生成的架构。

Dead Ends

Common approaches that don't work:

  1. 60% fail

    This only works if the database version is reset; if the schema is still incorrect, the error persists.

  2. 85% fail

    Room manages schema versions; manual changes cause version conflicts and data loss.

  3. 90% fail

    Room will throw an IllegalStateException about missing migration or fallback to destructive migration if configured.