Issue
I'm trying to persist a simple object that I I call QuesDto to a Room database.
I checked everything but couldn't figure out why I'm getting this exception:
FATAL EXCEPTION: pool-3-thread-1
Process: ovh.lahmadi.naditutoadmin, PID: 7753
java.lang.IllegalArgumentException: Version must be >= 1, was 0
at android.database.sqlite.SQLiteOpenHelper.<init>(SQLiteOpenHelper.java:164)
.......
Here is my Room Database abstract class:
@Database(entities = {QuesDto.class}, version = 0, exportSchema = false)
public abstract class QuizRoomDB extends RoomDatabase {
private static volatile QuizRoomDB INSTANCE;
public abstract QuizDao wordDao();
.....
public static QuizRoomDB getDatabase(Context context){
if (INSTANCE == null){
synchronized (QuizRoomDB.class){
if (INSTANCE == null){
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), QuizRoomDB.class, "quiz_db")
.build();
}
}
}
return INSTANCE;
}
}
My Dao interface :
@Dao
public interface QuizDao {
@Insert (onConflict = OnConflictStrategy.IGNORE)
void insert(QuesDto quesDto);
}
My Entity:
@Entity (tableName = "ques")
public class QuesDto {
@PrimaryKey
@NonNull
private long quesId;
private String quesHtml;
......
}
How I call the insert function:
public QuestionRepository(Application application) {
this.context = application.getApplicationContext();
.....
}
public void saveQuesLocally(QuesDto quesDto) {
QuizRoomDB db = QuizRoomDB.getDatabase(context);
QuizDao quizDao = db.wordDao();
Log.d(N.Room, "QuestionRepository. inside saveQuesLocally");
new Thread(() -> {
QuizRoomDB.dbWriteExecutor.execute(()-> quizDao.insert(quesDto));
}).start();
}
Thanks for your help.
Solution
The error is happening due to this internal logic:
if (version < 1) throw new IllegalArgumentException("Version must be >= 1, was " + version);
The SQLiteOpenHelper code is: https://android.googlesource.com/platform/frameworks/base/+/74f170f9468d3cf6d7d0ef453320141a3e63571b/core/java/android/database/sqlite/SQLiteOpenHelper.java
You will need to change the version 1
in your class:
@Database(entities = {QuesDto.class}, version = 1, exportSchema = false)
public abstract class QuizRoomDB extends RoomDatabase {
}
Answered By - SMA
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.