【发布时间】:2017-11-16 12:53:29
【问题描述】:
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.
【问题讨论】:
-
如果您正在更改数据库架构(即实体或其他内容),那么您必须在此行中增加版本
@Database(entities = {Profile.class, Medicine.class}, version = 1) -
看我需要向配置文件表输入数据。所以我不打算更改表的架构。
-
看我需要向配置文件表输入数据。所以我不打算更改表的架构。
-
这个问题几乎是 stackoverflow.com/questions/44197309/… 的重复检查该问题的第一个答案,您将获得一个更完整的答案,可以处理多种情况(例如发生此错误并且您已经拥有应用程序时)生产中)。
标签: android sqlite android-room