Java Code Examples for android.database.sqlite.SQLiteDatabase.getVersion()

The following are Jave code examples for showing how to use getVersion() of the android.database.sqlite.SQLiteDatabase class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: q-mail   File: StoreSchemaDefinition.java   View Source Code Vote up 6 votes
private void upgradeDatabase(final SQLiteDatabase db) {
    Timber.i("Upgrading database from version %d to version %d", db.getVersion(), DB_VERSION);

    db.beginTransaction();
    try {
        // schema version 29 was when we moved to incremental updates
        // in the case of a new db or a < v29 db, we blow away and start from scratch
        if (db.getVersion() < 29) {
            dbCreateDatabaseFromScratch(db);
        } else {
            RealMigrationsHelper migrationsHelper = new RealMigrationsHelper(localStore);
            Migrations.upgradeDatabase(db, migrationsHelper);
        }

        db.setVersion(LocalStore.DB_VERSION);

        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }

    if (db.getVersion() != LocalStore.DB_VERSION) {
        throw new RuntimeException("Database upgrade failed!");
    }
}
 
Example 2
Project: justintrain-client-android   File: SQLiteAssetHelper.java   View Source Code Vote up 6 votes
/**
 * Create and/or open a database.  This will be the same object returned by
 * {@link #getWritableDatabase} unless some problem, such as a full disk,
 * requires the database to be opened read-only.  In that case, a read-only
 * database object will be returned.  If the problem is fixed, a future call
 * to {@link #getWritableDatabase} may succeed, in which case the read-only
 * database object will be closed and the read/write object will be returned
 * in the future.
 *
 * <p class="caution">Like {@link #getWritableDatabase}, this method may
 * take a long time to return, so you should not call it from the
 * application main thread, including from
 * {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
 *
 * @return a database object valid until {@link #getWritableDatabase}
 * or {@link #close} is called.
 * @throws SQLiteException if the database cannot be opened
 */
@Override
public synchronized SQLiteDatabase getReadableDatabase() {
    if (mDatabase != null && mDatabase.isOpen()) {
        return mDatabase;  // The database is already open for business
    }

    if (mIsInitializing) {
        throw new IllegalStateException("getReadableDatabase called recursively");
    }

    try {
        return getWritableDatabase();
    } catch (SQLiteException e) {
        if (mName == null) throw e;  // Can't open a temp database read-only!
        Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
    }

    SQLiteDatabase db = null;
    try {
        mIsInitializing = true;
        String path = mContext.getDatabasePath(mName).getPath();
        db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);
        if (db.getVersion() != mNewVersion) {
            throw new SQLiteException("Can't upgrade read-only database from version " +
                    db.getVersion() + " to " + mNewVersion + ": " + path);
        }

        onOpen(db);
        Log.w(TAG, "Opened " + mName + " in read-only mode");
        mDatabase = db;
        return mDatabase;
    } finally {
        mIsInitializing = false;
        if (db != null && db != mDatabase) db.close();
    }
}
 
Example 3
Project: GitHub   File: DbManagerImpl.java   View Source Code Vote up 5 votes
public synchronized static DbManager getInstance(DaoConfig daoConfig) {

        if (daoConfig == null) {//使用默认配置
            daoConfig = new DaoConfig();
        }

        DbManagerImpl dao = DAO_MAP.get(daoConfig);
        if (dao == null) {
            dao = new DbManagerImpl(daoConfig);
            DAO_MAP.put(daoConfig, dao);
        } else {
            dao.daoConfig = daoConfig;
        }

        // update the database if needed
        SQLiteDatabase database = dao.database;
        int oldVersion = database.getVersion();
        int newVersion = daoConfig.getDbVersion();
        if (oldVersion != newVersion) {
            if (oldVersion != 0) {
                DbUpgradeListener upgradeListener = daoConfig.getDbUpgradeListener();
                if (upgradeListener != null) {
                    upgradeListener.onUpgrade(dao, oldVersion, newVersion);
                } else {
                    try {
                        dao.dropDb();
                    } catch (DbException e) {
                        LogUtil.e(e.getMessage(), e);
                    }
                }
            }
            database.setVersion(newVersion);
        }

        return dao;
    }
 
Example 4
Project: sqliteAsset   File: SQLiteAssetHelper.java   View Source Code Vote up 5 votes
/**
 * Create and/or open a database.  This will be the same object returned by
 * {@link #getWritableDatabase} unless some problem, such as a full disk,
 * requires the database to be opened read-only.  In that case, a read-only
 * database object will be returned.  If the problem is fixed, a future call
 * to {@link #getWritableDatabase} may succeed, in which case the read-only
 * database object will be closed and the read/write object will be returned
 * in the future.
 *
 * <p class="caution">Like {@link #getWritableDatabase}, this method may
 * take a long time to return, so you should not call it from the
 * application main thread, including from
 * {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
 *
 * @return a database object valid until {@link #getWritableDatabase}
 * or {@link #close} is called.
 * @throws SQLiteException if the database cannot be opened
 */
@Override
public synchronized SQLiteDatabase getReadableDatabase() {
    if (mDatabase != null && mDatabase.isOpen()) {
        return mDatabase;  // The database is already open for business
    }

    if (mIsInitializing) {
        throw new IllegalStateException("getReadableDatabase called recursively");
    }

    try {
        return getWritableDatabase();
    } catch (SQLiteException e) {
        if (mName == null) throw e;  // Can't open a temp database read-only!
        Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
    }

    SQLiteDatabase db = null;
    try {
        mIsInitializing = true;
        String path = mContext.getDatabasePath(mName).getPath();
        db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);
        if (db.getVersion() != mNewVersion) {
            throw new SQLiteException("Can't upgrade read-only database from version " +
                    db.getVersion() + " to " + mNewVersion + ": " + path);
        }

        onOpen(db);
        Log.w(TAG, "Opened " + mName + " in read-only mode");
        mDatabase = db;
        return mDatabase;
    } finally {
        mIsInitializing = false;
        if (db != null && db != mDatabase) db.close();
    }
}
 
Example 5
Project: q-mail   File: Migrations.java   View Source Code Vote up 4 votes
@SuppressWarnings("fallthrough")
public static void upgradeDatabase(SQLiteDatabase db, MigrationsHelper migrationsHelper) {
    switch (db.getVersion()) {
        case 29:
            MigrationTo30.addDeletedColumn(db);
        case 30:
            MigrationTo31.changeMsgFolderIdDeletedDateIndex(db);
        case 31:
            MigrationTo32.updateDeletedColumnFromFlags(db);
        case 32:
            MigrationTo33.addPreviewColumn(db);
        case 33:
            MigrationTo34.addFlaggedCountColumn(db);
        case 34:
            MigrationTo35.updateRemoveXNoSeenInfoFlag(db);
        case 35:
            MigrationTo36.addAttachmentsContentIdColumn(db);
        case 36:
            MigrationTo37.addAttachmentsContentDispositionColumn(db);
        case 37:
            // Database version 38 is solely to prune cached attachments now that we clear them better
        case 38:
            MigrationTo39.headersPruneOrphans(db);
        case 39:
            MigrationTo40.addMimeTypeColumn(db);
        case 40:
            MigrationTo41.db41FoldersAddClassColumns(db);
            MigrationTo41.db41UpdateFolderMetadata(db, migrationsHelper);
        case 41:
            boolean notUpdatingFromEarlierThan41 = db.getVersion() == 41;
            if (notUpdatingFromEarlierThan41) {
                MigrationTo42.from41MoveFolderPreferences(migrationsHelper);
            }
        case 42:
            MigrationTo43.fixOutboxFolders(db, migrationsHelper);
        case 43:
            MigrationTo44.addMessagesThreadingColumns(db);
        case 44:
            MigrationTo45.changeThreadingIndexes(db);
        case 45:
            MigrationTo46.addMessagesFlagColumns(db, migrationsHelper);
        case 46:
            MigrationTo47.createThreadsTable(db);
        case 47:
            MigrationTo48.updateThreadsSetRootWhereNull(db);
        case 48:
            MigrationTo49.createMsgCompositeIndex(db);
        case 49:
            MigrationTo50.foldersAddNotifyClassColumn(db, migrationsHelper);
        case 50:
            MigrationTo51.db51MigrateMessageFormat(db, migrationsHelper);
        case 51:
            MigrationTo52.addMoreMessagesColumnToFoldersTable(db);
        case 52:
            MigrationTo53.removeNullValuesFromEmptyColumnInMessagesTable(db);
        case 53:
            MigrationTo54.addPreviewTypeColumn(db);
        case 54:
            MigrationTo55.createFtsSearchTable(db, migrationsHelper);
        case 55:
            MigrationTo56.cleanUpFtsTable(db);
        case 56:
            MigrationTo57.fixDataLocationForMultipartParts(db);
        case 57:
            MigrationTo58.cleanUpOrphanedData(db);
            MigrationTo58.createDeleteMessageTrigger(db);
        case 58:
            MigrationTo59.addMissingIndexes(db);
        case 59:
            MigrationTo60.migratePendingCommands(db);
        case 60:
            MigrationTo61.addFolderRemoteId(db);
        case 61:
            MigrationTo62.addFolderParentRemoteId(db);
        case 63:
            MigrationTo63.removeErrorsFolder(db);
    }
}