web-dev-qa-db-ja.com

ルームはAndroidでデータの整合性を検証できません

MainActivityクラス

public class MainActivity extends BaseActivity {

    private AppDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Database creation
        db = Room.databaseBuilder(getApplicationContext(),
                AppDatabase.class, "Medimap-db").build();
//        Profile profile=new Profile("rajitha","12-2-345","male","no 345","test med","test emergency","testurl");
//        db.profileDao().insert(profile);
//        Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
        new DatabaseAsync().execute();


    }

    private class DatabaseAsync extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            //Perform pre-adding operation here.
        }

        @Override
        protected Void doInBackground(Void... voids) {
            //Let's add some dummy data to the database.
            Profile profile = new Profile("rajitha", "12-2-345", "male", "no 345", "test med", "test emergency", "testurl");
            db.profileDao().insert(profile);


            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_SHORT).show();
            //To after addition operation here.
        }
    }

}

AppDatabaseクラス

   @Database(entities = {Profile.class, Medicine.class}, version = 1)
    public abstract class AppDatabase extends RoomDatabase {
        public abstract ProfileDao profileDao();
        public abstract MedicineDao medicineDaoDao();
    }

プロフィール

@Dao
public interface ProfileDao {

    @Query("SELECT * FROM profile")
    List<Profile> getAll();

//    @Query("SELECT * FROM user WHERE uid IN (:userIds)")
//    List<Profile> loadAllByIds(int[] userIds);

//    @Query("SELECT * FROM user WHERE first_name LIKE :first AND "
//            + "last_name LIKE :last LIMIT 1")
//    User findByName(String first, String last);

    @Insert
    void insertAll(Profile... profiles);

    @Insert
    void insert(Profile profile);

    @Delete
    void delete(Profile profile);
}

ここでは、アプリの最初の実行後にエラーが発生しました。アプリがもう一度DATABASEを作成しようとしているようですが、既存のものがあるため、バージョンコードを変更することを提案しました。私の要件は、新しいデータセットを挿入するだけです。どうすればそれを達成できますか?前もって感謝します。 logcatエラーは次のとおりです。

Caused by: Java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.
18
Rajitha Perera

現時点でアプリケーションを開発しているだけの場合は、本番環境ではなく、デバイスからアプリをアンインストールしてから再インストールすると、必要に応じて機能することを意味します。

23
VinayagaSundar

Android 6.0以降

数日前にこの問題に遭遇した人は、単にアンインストールしても役に立たない可能性がありますので、理解する前に問題を共有しましょう:GoogleはAndroid 6.0。再インストールするとデータベースが復元されます。 ( https://developer.Android.com/guide/topics/data/autobackup.html

あなたは単に追加することができます...

<application ...
    Android:allowBackup="false">
</app>

...この機能を無効にすると、すぐに使用できます(データベースを削除するには、単にアンインストールできます)。

32
g-mac

エンティティの更新時に移行を気にしない場合:

  1. データベースのバージョンを上げる
  2. データベースの構築時にfallbackToDestructiveMigration()メソッドを追加します。例:

    Room.databaseBuilder(appContext、AppDatabase.class、 "appdb.db").fallbackToDestructiveMigration().build();

これが誰かを助けることを願っています;)

7
Hemant Kaushik

バージョンを上げる:

@Database(entities = {EmployeeEntry.class}, version = 2, exportSchema = false)

link から取得

5
Hamid

私の場合、再インストールしても解決せず、Android:allowBackup:true使用したライブラリの1つで必要だったため。そこで、[設定]> [アプリ]> [アプリ]に移動し、そのアプリのデータとメモリを消去しました。問題はなくなりました;)

2
Baftek

ステップ1:新しいビルドをインストールする

手順2:[設定]からすべてのアプリデータを消去します。

ステップ3:アプリを開く(現在は正常に動作します)

0
Hangman