LeDon
LeDon

Reputation: 549

Alter Table in onUpgrade

I got a small problem with my onUpgrade function. The Code is as follows:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if(oldVersion<2){
        db.rawQuery("ALTER TABLE "+this.getTableName()+" ADD COLUMN "+COLUMNS.TIME+ " integer default 0;", null);
        db.rawQuery("ALTER TABLE "+this.getTableName()+" ADD COLUMN "+COLUMNS.DATE+ " integer default 0;", null);
        db.rawQuery("UPDATE "+this.getTableName()+ " SET "+COLUMNS.TIME+ "="+COLUMNS.TIME_OLD+";", null);
        db.rawQuery("UPDATE "+this.getTableName()+ " SET "+COLUMNS.DATE+ "="+COLUMNS.DATE_OLD+";", null);
    }
};

The onUpgrade function is obviously called right because I get the error:

no such column: _timenew (code 1): , while compiling: UPDATE TABLE_SCORE_NORMAL SET _timenew=_time;

I wonder why it is this way. If I try this manually on the console it works perfectly.

Thanks in advance.

Upvotes: 1

Views: 1273

Answers (2)

laalto
laalto

Reputation: 152847

rawQuery() compiles the SQL but does not run it. You'll need to call one of the moveTo...() methods on the returned Cursor to run the compiled SQL.

execSql() both compiles and runs the SQL.

Upvotes: 1

sercxjo
sercxjo

Reputation: 358

Insert between ALTER and UPDATE:

db.setTransactionSuccessful();
db.endTransaction();
db.beginTransaction();

Upvotes: 0

Related Questions