I have a SQLite DB in my application and I'm trying to migrate it from version 4 to version 5. The aplication is working fine if I uninstall and then install again. I'll resume the problem for easy understanding.
In v4 I created this table:
private static final String CREATE_TABLE = "CREATE TABLE " + Globals._TABLE +
" ( " +
Globals._ID + " INTEGER PRIMARY KEY, " +
Globals._PRIORITY + " INTEGER, " +
Globals._FUNC_NAME + " TEXT, " +
");";
In v5, I removed the columns _PRIORITY and _FUNC_NAME. I was also indexing with the index provided by my MySQL DB. I needed to create a way to index the DB internally. So I created the same table like this:
private static final String CREATE_TABLE =
"CREATE TABLE IF NOT EXISTS " + Globals._TABLE +
" ( " +
Globals._SQLITE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
Globals._UPLOADED + " INTEGER, " +
Globals._ID + " INTEGER DEFAULT 0, " +
");";
My OnCreate method is this and is not throwing any errors:
@Override
public void onCreate( SQLiteDatabase db )
{
try
{
db.execSQL( CREATE_TABLE );
}
catch( SQLException e )
{
e.printStackTrace( );
}
}
I'm dropping the table with this query:
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + Globals._TABLE;
My OnUpgrade method is this and is also not throwing errors:
SQLiteMigratorV5 migrator = new SQLiteMigratorV5( mContext, db );
switch( newVersion )
{
case 5:
migrator.saveData( );
break;
default:
break;
}
try
{
db.execSQL( DROP_TABLE );
}
catch( SQLException e )
{
e.printStackTrace( );
}
onCreate( db );
switch( newVersion )
{
case 5:
migrator.restoreData( );
break;
default:
break;
}
}
Debugging the application I can see both onUpgrade and then OnCreate are being called. My class responsible for migration saves all the data, and was supposed to restore everything to the new tables. Debugging I can also say that all the rows from the old table are being correctly inserted in new table.
The problems reside when I try to access this data. I had other table that indexed elements from this first table by the id, I'm also recreating this other table and I need to index with the SQLite id. So I'm executing a rawQuery and retrieving a cursor:
String query = "SELECT * FROM " + Globals._TABLE + " WHERE " + Globals._ID + " = '1'";
Cursor cursor = mDB.rawQuery( query, null );
When I check the cursor columns I cannot find _SQLITE_ID neither _UPLOADED but I can find _PRIORITY and _FUNC_NAME, and that leads me to errors when I try to get the newly inserted row _SQLITE_ID to use in my other table.
for( cursor.moveToFirst( ); ! cursor.isAfterLast( ); cursor.moveToNext( ) )
{
setSQLiteId( cursor.getInt( cursor.getColumnIndex( Globals._SQLITE_ID ) ) );
...
}
This piece of code leads me to this error:
java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
I checked everything more than once and I couldn't understand what is happening, my only assumption is that the old tables are not being dropped and the new ones are not being created because of the CREATE TABLE IF NOT EXISTS.
Anyone can give me a light on how to solve this problem?
Aucun commentaire:
Enregistrer un commentaire