I am trying to migrate my existing data in sqlite to room database.
I have provided a migration rule .
This is the migration rule:
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("CREATE TABLE temp_account (\n"+
" _id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"
+ "name TEXT NOT NULL UNIQUE, "
+ "number TEXT DEFAULT 0, "
+ "balance REAL DEFAULT 0 NOT NULL, "
+ "account_type TEXT);");
database.execSQL("INSERT INTO temp_account( name,number,balance,account_type) " +
"SELECT name,number,balance,account_type FROM account");
database.execSQL("DROP TABLE account");
database.execSQL("ALTER TABLE temp_account RENAME TO account;");
;
Below is my original table which I want to migrate:
"CREATE TABLE account (\n"+
" _id INTEGER PRIMARY KEY AUTOINCREMENT ,"
+ "name TEXT NOT NULL UNIQUE, "
+ "number TEXT DEFAULT 0, "
+ "balance REAL , "
+ "account_type TEXT);");
While it worked in most of the devices but the app crashes in few devices.Crashes are not specific to any device or android version.Below is error report from Google:
java.lang.RuntimeException:
at android.os.AsyncTask$3.done (AsyncTask.java:354)
at java.util.concurrent.FutureTask.finishCompletion (FutureTask.java:383)
at java.util.concurrent.FutureTask.setException (FutureTask.java:252)
at java.util.concurrent.FutureTask.run (FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run (AsyncTask.java:245)
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: android.database.sqlite.SQLiteConstraintException: at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:796)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
Any help is highly appreciated.