Example usage for android.database.sqlite SQLiteDatabase endTransaction

List of usage examples for android.database.sqlite SQLiteDatabase endTransaction

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase endTransaction.

Prototype

public void endTransaction() 

Source Link

Document

End a transaction.

Usage

From source file:com.jefftharris.passwdsafe.NotificationMgr.java

/** Load the expiration entries */
private void loadEntries() {
    SQLiteDatabase db = itsDbHelper.getWritableDatabase();
    try {//  ww w .  j  a  va 2s  . com
        db.beginTransaction();
        loadEntries(db);
        db.setTransactionSuccessful();
    } catch (SQLException e) {
        Log.e(TAG, "Database error", e);
    } finally {
        db.endTransaction();
    }
}

From source file:org.mariotaku.twidere.util.content.DatabaseUpgradeHelper.java

public static void safeUpgrade(final SQLiteDatabase db, final String table, final String[] newColNames,
        final String[] newColTypes, final boolean dropDirectly, final boolean strictMode,
        final Map<String, String> colAliases, final OnConflict onConflict) {

    if (newColNames == null || newColTypes == null || newColNames.length != newColTypes.length)
        throw new IllegalArgumentException(
                "Invalid parameters for upgrading table " + table + ", length of columns and types not match.");

    // First, create the table if not exists.
    final NewColumn[] newCols = NewColumn.createNewColumns(newColNames, newColTypes);
    final String createQuery = createTable(true, table).columns(newCols).buildSQL();
    db.execSQL(createQuery);//from   ww w . java2  s . c  o m

    // We need to get all data from old table.
    final String[] oldCols = getColumnNames(db, table);
    if (strictMode) {
        final String oldCreate = getCreateSQL(db, table);
        final Map<String, String> map = getTypeMapByCreateQuery(oldCreate);
        boolean different = false;
        for (final NewColumn newCol : newCols) {
            if (!newCol.getType().equalsIgnoreCase(map.get(newCol.getName()))) {
                different = true;
            }
        }
        if (!different)
            return;
    } else if (oldCols == null || TwidereArrayUtils.contentMatch(newColNames, oldCols))
        return;
    if (dropDirectly) {
        db.beginTransaction();
        db.execSQL(dropTable(true, table).getSQL());
        db.execSQL(createQuery);
        db.setTransactionSuccessful();
        db.endTransaction();
        return;
    }
    final String tempTable = String.format(Locale.US, "temp_%s_%d", table, System.currentTimeMillis());
    db.beginTransaction();
    db.execSQL(alterTable(table).renameTo(tempTable).buildSQL());
    db.execSQL(createQuery);
    final String[] notNullCols = getNotNullColumns(newCols);
    final String insertQuery = createInsertDataQuery(table, tempTable, newColNames, oldCols, colAliases,
            notNullCols, onConflict);
    if (insertQuery != null) {
        db.execSQL(insertQuery);
    }
    db.execSQL(dropTable(true, tempTable).getSQL());
    db.setTransactionSuccessful();
    db.endTransaction();
}

From source file:com.cloudmine.api.db.RequestDBOpenHelper.java

public void insertRequest(ContentValues requestValues, ContentValues[] headerValues) {
    SQLiteDatabase db = getWritableDatabase();
    db.beginTransaction();/*  w  w  w.  j a va2 s. com*/
    try {
        long requestId = db.insertOrThrow(REQUEST_DATABASE_TABLE, null, requestValues);
        throwIfFailed(requestId);
        for (ContentValues headerValue : headerValues) {
            headerValue.put(KEY_HEADER_REQUEST_FK, requestId);
            long result = db.insertOrThrow(HEADER_DATABASE_TABLE, null, headerValue);
            throwIfFailed(result);
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}

From source file:org.opendatakit.common.android.database.DataModelDatabaseHelper.java

public static void deleteTableAndData(SQLiteDatabase db, String formId) {
    try {//from   w w  w.  j  av  a 2s .c o m
        IdInstanceNameStruct ids = getIds(db, formId);

        String whereClause = TableDefinitionsColumns.TABLE_ID + " = ?";
        String[] whereArgs = { ids.tableId };

        db.beginTransaction();

        // Drop the table used for the formId
        db.execSQL("DROP TABLE IF EXISTS " + ids.tableId + ";");

        // Delete the table definition for the tableId
        int count = db.delete(TABLE_DEFS_TABLE_NAME, whereClause, whereArgs);

        // Delete the column definitions for this tableId
        db.delete(COLUMN_DEFINITIONS_TABLE_NAME, whereClause, whereArgs);

        // Delete the uploads for the tableId
        String uploadWhereClause = InstanceColumns.DATA_TABLE_TABLE_ID + " = ?";
        db.delete(UPLOADS_TABLE_NAME, uploadWhereClause, whereArgs);

        // Delete the values from the 4 key value stores
        db.delete(KEY_VALUE_STORE_DEFAULT_TABLE_NAME, whereClause, whereArgs);
        db.delete(KEY_VALUE_STORE_ACTIVE_TABLE_NAME, whereClause, whereArgs);
        db.delete(KEY_VALUE_STORE_SERVER_TABLE_NAME, whereClause, whereArgs);
        db.delete(KEY_VALULE_STORE_SYNC_TABLE_NAME, whereClause, whereArgs);

        db.setTransactionSuccessful();

    } catch (Exception ex) {
        Log.e(t, "Exception during deletion of data for formId:" + formId + " exception: " + ex.toString());
    } finally {
        db.endTransaction();
    }
}

From source file:org.getlantern.firetweet.util.content.DatabaseUpgradeHelper.java

public static void safeUpgrade(final SQLiteDatabase db, final String table, final String[] newColNames,
        final String[] newColTypes, final boolean dropDirectly, final boolean strictMode,
        final Map<String, String> colAliases, final OnConflict onConflict) {

    if (newColNames == null || newColTypes == null || newColNames.length != newColTypes.length)
        throw new IllegalArgumentException(
                "Invalid parameters for upgrading table " + table + ", length of columns and types not match.");

    // First, create the table if not exists.
    final NewColumn[] newCols = NewColumn.createNewColumns(newColNames, newColTypes);
    final String createQuery = createTable(true, table).columns(newCols).buildSQL();
    db.execSQL(createQuery);//from   w  ww  . j a v  a 2  s .co  m

    // We need to get all data from old table.
    final String[] oldCols = getColumnNames(db, table);
    if (strictMode) {
        final String oldCreate = getCreateSQL(db, table);
        final Map<String, String> map = getTypeMapByCreateQuery(oldCreate);
        boolean differenct = false;
        for (final NewColumn newCol : newCols) {
            if (!newCol.getType().equalsIgnoreCase(map.get(newCol.getName()))) {
                differenct = true;
            }
        }
        if (!differenct)
            return;
    } else if (oldCols == null || FiretweetArrayUtils.contentMatch(newColNames, oldCols))
        return;
    if (dropDirectly) {
        db.beginTransaction();
        db.execSQL(dropTable(true, table).getSQL());
        db.execSQL(createQuery);
        db.setTransactionSuccessful();
        db.endTransaction();
        return;
    }
    final String tempTable = String.format(Locale.US, "temp_%s_%d", table, System.currentTimeMillis());
    db.beginTransaction();
    db.execSQL(alterTable(table).renameTo(tempTable).buildSQL());
    db.execSQL(createQuery);
    final String[] notNullCols = getNotNullColumns(newCols);
    final String insertQuery = createInsertDataQuery(table, tempTable, newColNames, oldCols, colAliases,
            notNullCols, onConflict);
    if (insertQuery != null) {
        db.execSQL(insertQuery);
    }
    db.execSQL(dropTable(true, tempTable).getSQL());
    db.setTransactionSuccessful();
    db.endTransaction();
}

From source file:ru.orangesoftware.financisto2.db.MyEntityManager.java

public long saveOrUpdate(Currency currency) {
    SQLiteDatabase db = db();
    db.beginTransaction();/*from   w  ww. ja  va  2  s . c o m*/
    try {
        if (currency.isDefault) {
            db.execSQL(UPDATE_DEFAULT_FLAG);
        }
        long id = super.saveOrUpdate(currency);
        db.setTransactionSuccessful();
        return id;
    } finally {
        db.endTransaction();
    }
}

From source file:com.jefftharris.passwdsafe.NotificationMgr.java

/**
 * Clear all notifications after being confirmed
 *///  w w w .  j  av a 2 s  .co m
public void handleClearAllConfirmed() {
    try {
        SQLiteDatabase db = itsDbHelper.getWritableDatabase();
        try {
            db.beginTransaction();
            db.delete(DB_TABLE_EXPIRYS, null, null);
            db.delete(DB_TABLE_URIS, null, null);
            loadEntries(db);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } catch (SQLException e) {
        Log.e(TAG, "Database error", e);
    }
}

From source file:github.popeen.dsub.util.SongDBHandler.java

protected synchronized void addSongsImpl(SQLiteDatabase db, int serverKey, List<Pair<String, String>> entries) {
    db.beginTransaction();// w w  w . j av a 2s .  c  om
    try {
        for (Pair<String, String> entry : entries) {
            ContentValues values = new ContentValues();
            values.put(SONGS_SERVER_KEY, serverKey);
            values.put(SONGS_SERVER_ID, entry.getFirst());
            values.put(SONGS_COMPLETE_PATH, entry.getSecond());

            db.insertWithOnConflict(TABLE_SONGS, null, values, SQLiteDatabase.CONFLICT_IGNORE);
        }

        db.setTransactionSuccessful();
    } catch (Exception e) {
    }

    db.endTransaction();
}

From source file:ru.orangesoftware.financisto2.db.MyEntityManager.java

public void deleteProject(long id) {
    SQLiteDatabase db = db();
    db.beginTransaction();/*from www .  ja va2  s.c  om*/
    try {
        delete(Project.class, id);
        ContentValues values = new ContentValues();
        values.put("project_id", 0);
        db.update("transactions", values, "project_id=?", new String[] { String.valueOf(id) });
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}

From source file:com.jefftharris.passwdsafe.NotificationMgr.java

public void passwdFileDataChanged(PasswdFileData fileData) {
    try {//from  w  w  w .  ja va2s.c  om
        SQLiteDatabase db = itsDbHelper.getWritableDatabase();
        try {
            db.beginTransaction();
            Long id = getDbUriId(fileData.getUri(), db);
            if (id != null) {
                doUpdatePasswdFileData(id, fileData, db);
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } catch (SQLException e) {
        Log.e(TAG, "Database error", e);
    }
}