Code example for SQLiteDatabase

Methods: execSQL, longForQuery

0
     * This code is only called when upgrading from an old calendar version, 
     * so there is no problem if sync state version 3 gets used again in the 
     * future. 
     */ 
    private void upgradeSyncState(SQLiteDatabase db) {
        long version = DatabaseUtils.longForQuery(db,
                 "SELECT " + SYNC_STATE_META_VERSION_COLUMN
                 + " FROM " + Tables.SYNC_STATE_META,
                 null); 
        if (version == PRE_FROYO_SYNC_STATE_VERSION) {
            Log.i(TAG, "Upgrading calendar sync state table");
            db.execSQL("CREATE TEMPORARY TABLE state_backup(_sync_account TEXT, "
                    + "_sync_account_type TEXT, data TEXT);"); 
            db.execSQL("INSERT INTO state_backup SELECT _sync_account, _sync_account_type, data"
                    + " FROM " 
                    + Tables.SYNC_STATE
                    + " WHERE _sync_account is not NULL and _sync_account_type is not NULL;"); 
            db.execSQL("DROP TABLE " + Tables.SYNC_STATE + ";");
            mSyncState.onDatabaseOpened(db);
            db.execSQL("INSERT INTO " + Tables.SYNC_STATE + "("
                    + SyncStateContract.Columns.ACCOUNT_NAME + ","
                    + SyncStateContract.Columns.ACCOUNT_TYPE + ","
                    + SyncStateContract.Columns.DATA
                    + ") SELECT _sync_account, _sync_account_type, data from state_backup;"); 
            db.execSQL("DROP TABLE state_backup;");
        } else { 
            // Wrong version to upgrade. 
            // Don't need to do anything more here because mSyncState.onDatabaseOpened() will blow 
            // away and recreate  the database (which will result in a resync). 
            Log.w(TAG, "upgradeSyncState: current version is " + version + ", skipping upgrade.");