Code example for SQLiteDatabase

Methods: beginTransactionendTransactionexecSQLsetTransactionSuccessful

0
 
        @Override 
        public void onCreate(SQLiteDatabase db) {
            if (LOGD) Log.d(LOG_TAG, "creating new launcher database");
             
            db.execSQL("CREATE TABLE favorites (" +
                    "_id INTEGER PRIMARY KEY," + 
                    "title TEXT," + 
                    "intent TEXT," + 
                    "container INTEGER," + 
                    "screen INTEGER," + 
                    "cellX INTEGER," + 
                    "cellY INTEGER," + 
                    "spanX INTEGER," + 
                    "spanY INTEGER," + 
                    "itemType INTEGER," + 
                    "appWidgetId INTEGER NOT NULL DEFAULT -1," + 
                    "isShortcut INTEGER," + 
                    "iconType INTEGER," + 
                    "iconPackage TEXT," + 
                    "iconResource TEXT," + 
                    "icon BLOB," + 
                    "uri TEXT," + 
                    "displayMode INTEGER" + 
                    ");"); 
 
            db.execSQL("CREATE TABLE gestures (" +
                    "_id INTEGER PRIMARY KEY," + 
                    "title TEXT," + 
                    "intent TEXT," + 
                    "itemType INTEGER," + 
                    "iconType INTEGER," + 
                    "iconPackage TEXT," + 
                    "iconResource TEXT," + 
                    "icon BLOB" + 
                    ");"); 
 
            // Database was just created, so wipe any previous widgets 
            if (mAppWidgetHost != null) {
                mAppWidgetHost.deleteHost();
                sendAppWidgetResetNotify(); 
            } 
             
            if (!convertDatabase(db)) {
                // Populate favorites table with initial favorites 
                loadFavorites(db);
            } 
        } 
 
        private boolean convertDatabase(SQLiteDatabase db) {
            if (LOGD) Log.d(LOG_TAG, "converting database from an older format, but not onUpgrade");
            boolean converted = false;
 
            final Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
                    "/old_favorites?notify=true"); 
            final ContentResolver resolver = mContext.getContentResolver();
            Cursor cursor = null;
 
            try { 
                cursor = resolver.query(uri, null, null, null, null);
            } catch (Exception e) {
	            // Ignore 
            } 
 
            // We already have a favorites database in the old provider 
            if (cursor != null && cursor.getCount() > 0) {
                try { 
                    converted = copyFromCursor(db, cursor) > 0;
                } finally { 
                    cursor.close();
                } 
 
                if (converted) {
                    resolver.delete(uri, null, null);
                } 
            } 
             
            if (converted) {
                // Convert widgets from this import into widgets 
                if (LOGD) Log.d(LOG_TAG, "converted and now triggering widget upgrade");
                convertWidgets(db);
            } 
 
            return converted;
        } 
 
        private int copyFromCursor(SQLiteDatabase db, Cursor c) {
            final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
            final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
            final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
            final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
            final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
            final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
            final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
            final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
            final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
            final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
            final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
            final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
            final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
            final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
 
            ContentValues[] rows = new ContentValues[c.getCount()];
            int i = 0;
            while (c.moveToNext()) {
                ContentValues values = new ContentValues(c.getColumnCount());
                values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
                values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
                values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
                values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
                values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
                values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
                values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
                values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
                values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
                values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
                values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
                values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
                values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
                values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
                values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
                rows[i++] = values;
            } 
 
            db.beginTransaction();
            int total = 0;
            try { 
                int numValues = rows.length;
                for (i = 0; i < numValues; i++) {
                    if (db.insert(TABLE_FAVORITES, null, rows[i]) < 0) {
                        return 0; 
                    } else { 
                        total++;
                    } 
                } 
                db.setTransactionSuccessful();
            } finally { 
                db.endTransaction();
            } 
 
            return total;
        } 
 
        @Override 
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if (LOGD) Log.d(LOG_TAG, "onUpgrade triggered");
             
            int version = oldVersion;
            if (version < 3) {
                // upgrade 1,2 -> 3 added appWidgetId column 
                db.beginTransaction();
                try { 
                    // Insert new column for holding appWidgetIds 
                    db.execSQL("ALTER TABLE favorites " +
                        "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;"); 
                    db.setTransactionSuccessful();
                    version = 3;
                } catch (SQLException ex) {
                    // Old version remains, which means we wipe old data 
                    Log.e(LOG_TAG, ex.getMessage(), ex);
                } finally { 
                    db.endTransaction();
                } 
                 
                // Convert existing widgets only if table upgrade was successful 
                if (version == 3) {
                    convertWidgets(db);
                } 
            } 
 
            if (version < 4) {
                db.beginTransaction();
                try { 
                    db.execSQL("CREATE TABLE gestures (" +
                        "_id INTEGER PRIMARY KEY," + 
                        "title TEXT," + 
                        "intent TEXT," + 
                        "itemType INTEGER," + 
                        "iconType INTEGER," + 
                        "iconPackage TEXT," + 
                        "iconResource TEXT," + 
                        "icon BLOB" + 
                        ");"); 
                    db.setTransactionSuccessful();
                    version = 4;
                } catch (SQLException ex) {
                    // Old version remains, which means we wipe old data 
                    Log.e(LOG_TAG, ex.getMessage(), ex);
                } finally { 
                    db.endTransaction();
                } 
            } 
             
            if (version != DATABASE_VERSION) {
                Log.w(LOG_TAG, "Destroying all old data.");
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_GESTURES);
                onCreate(db);
            } 
        } 
         
        /** 
Stop searching for code, let great code find you!  Add Codota to your java IDE