Nima Khalili
Nima Khalili

Reputation: 251

Room database error : Migration didn't properly handle

I want to use Room library for my database application. I want open a database from assets folder and get all rows.

I used this way for open database : https://github.com/humazed/RoomAsset .

Error :

    java.lang.IllegalStateException: Migration didn't properly handle quiz(com.example.user.testroomlibrary3.Quiz).
 Expected:
TableInfo{name='quiz', columns={quiz_date=Column{name='quiz_date', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, class_id=Column{name='class_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1}, quiz_name=Column{name='quiz_name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
 Found:
TableInfo{name='quiz', columns={class_id=Column{name='class_id', type='int', affinity='3', notNull=false, primaryKeyPosition=1}, quiz_date=Column{name='quiz_date', type='date', affinity='1', notNull=false, primaryKeyPosition=0}, quiz_name=Column{name='quiz_name', type='nvarchar(50)', affinity='2', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}

My code :

Main :

public class MainActivity extends AppCompatActivity {

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

    final AppDatabase db = AppDatabase.getAppDatabase(MainActivity.this);

    new Thread(new Runnable() {
        @Override
        public void run() {
            List<Quiz> quizList = db.quizDao().getAll();
            Log.d("Main", "list = " + quizList);
        }
    }).start();
}

Quiz :

@Entity(tableName = "quiz")
public class Quiz {

    @PrimaryKey
    @ColumnInfo(name = "class_id")
    private int id;

    @ColumnInfo(name = "quiz_name")
    private String name;

    @ColumnInfo(name = "quiz_date")
    private String date;
}

QuizDao :

@Dao
public interface QuizDao {
    @Query("SELECT * FROM quiz")
    List<Quiz> getAll();
}

AppDatabase :

public abstract class AppDatabase extends RoomDatabase {

private static AppDatabase INSTANCE;

public abstract QuizDao quizDao();

public static AppDatabase getAppDatabase(Context context)
{
    if(INSTANCE == null)
    {
        INSTANCE = RoomAsset.databaseBuilder(context.getApplicationContext() ,
                AppDatabase.class , "TEST.db").build();
    }
    return INSTANCE;
}
}

Table query :

CREATE TABLE quiz(
    class_id int primary key,
    quiz_name nvarchar(50) ,
    quiz_date date);

Upvotes: 3

Views: 3877

Answers (2)

MikeT
MikeT

Reputation: 57063

The problem is that Room expects columns in a table to have specific column types.

Those being either INTEGER, TEXT, BLOB or REAL (which is used is dependant upon the java type defined in the Entity for the column).

As such by saying that it expected :-

quiz_name=Column{name='quiz_name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}

but found :-

quiz_name=Column{name='quiz_name', type='nvarchar(50)', affinity='2', notNull=false, primaryKeyPosition=0}

It is saying that instead of nvarchar(50) it is expecting TEXT to be used.

As such you need to convert the table that is copied into the assets folder to use the column definition :-

quiz_name TEXT 

Upvotes: 3

Nima Khalili
Nima Khalili

Reputation: 251

The problem was table query . According to use Room database need to edit table query. After edit query , application is run with no any problem

Edit Table query :

CREATE TABLE quiz(
    class_id INTEGER primary key not null,
    quiz_name TEXT ,
    quiz_date TEXT)

Upvotes: 0

Related Questions