【问题标题】:Error inputing data into the database using Room, Dao使用 Room、Dao 将数据输入数据库时​​出错
【发布时间】:2019-08-01 11:55:34
【问题描述】:

这是我第一次使用 Room 和 LiveData。我不断收到有关使用 Dao(数据库访问对象)将数据输入数据库的错误。我已经尝试更新 build.gradle 文件,但它并没有解决错误。它说“递归调用getDatabase”。

下面是我的logcat:

 2019-08-01 13:35:55.644 18117-18149/com.example.inventory E/AndroidRuntime: FATAL EXCEPTION: arch_disk_io_0
    Process: com.example.inventory, PID: 18117
    java.lang.RuntimeException: Exception while computing database live data.
        at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:92)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.lang.IllegalStateException: getDatabase called recursively
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:338)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:298)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:92)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:53)
        at androidx.room.RoomDatabase.inTransaction(RoomDatabase.java:452)
        at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.java:275)
        at com.example.inventory.data.InventoryDao_Impl.insert(InventoryDao_Impl.java:58)
        at com.example.inventory.data.InventoryRoomDatabase$PopulateDbAsync.<init>(InventoryRoomDatabase.java:46)
        at com.example.inventory.data.InventoryRoomDatabase$1.onOpen(InventoryRoomDatabase.java:34)
        at com.example.inventory.data.InventoryRoomDatabase_Impl$1.onOpen(InventoryRoomDatabase_Impl.java:58)
        at androidx.room.RoomOpenHelper.onOpen(RoomOpenHelper.java:120)

房间数据库类

@Database(entities = Product.class, version = 1, exportSchema = false)
public abstract class InventoryRoomDatabase extends RoomDatabase {

    public abstract InventoryDao inventoryDao();

    public static InventoryRoomDatabase INSTANCE;

    public static InventoryRoomDatabase getDatabase(final Context context) {
        synchronized (InventoryRoomDatabase.class) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                        InventoryRoomDatabase.class, "inventory_table")
                        .fallbackToDestructiveMigration().addCallback(roomDatabaseCallback).build();
            }
        }
        return INSTANCE;
    }

    private static RoomDatabase.Callback roomDatabaseCallback = new RoomDatabase.Callback() {
        @Override
        public void onOpen(@NonNull SupportSQLiteDatabase db) {
            super.onOpen(db);
            new PopulateDbAsync(INSTANCE).execute();

        }
    };

    private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {

        private final InventoryDao mDao;
        Product product = new Product("Ice cream", 0.99, 70);

        PopulateDbAsync(InventoryRoomDatabase db) {
            mDao = db.inventoryDao();
            mDao.insert(product);
        }

        @Override
        protected Void doInBackground(Void... voids) {
            return null;
        }
    }
}

道接口

@Dao
public interface InventoryDao {

    @Insert
    void insert(Product product);

    @Query("DELETE FROM inventory_table")
    void deleteAll();

    @Query("SELECT * from inventory_table")
    LiveData<List<Product>> getAllProducts();
}

【问题讨论】:

  • 你在哪里调用 getDatabase()?

标签: android android-room android-livedata


【解决方案1】:

您的数据库数据插入过程正在主线程上工作,并且没有权限访问主线程上的房间数据库,要访问主线程上的数据库,请尝试以下代码。

 Room.databaseBuilder(AppInjector.getApplication(), AppDb.class, DbConstant.DATABASENAME)
                .fallbackToDestructiveMigration()
                .allowMainThreadQueries()
                .build() 

【讨论】:

    【解决方案2】:

    在我的情况下,实体已经改变,当我编译它时,它给出的数据库与我试图制作的实体无法比较,这就是为什么在我卸载应用程序后它给我这个错误,它停止给出那个错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      • 2019-08-21
      • 2017-11-20
      相关资源
      最近更新 更多