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.alchemiasoft.common.content.BookDBOpenHelper.java

@Override
public void onCreate(SQLiteDatabase db) {
    try {/*from   w  w w  .java  2  s  .  c  o m*/
        db.beginTransaction();

        db.execSQL(BookDB.Book.CREATE_TABLE);

        String input = null;
        try {
            input = ResUtil.assetAsString(mContext, Constants.BOOKS_PATH);
        } catch (Exception e) {
            Log.e(TAG_LOG, "Cannot read the input at assets/" + Constants.BOOKS_PATH);
        }
        // Adding the default entries
        if (!TextUtils.isEmpty(input)) {
            Log.d(TAG_LOG, "Adding into the DB: " + input);
            final JSONArray arr = new JSONArray(input);
            final List<Book> books = Book.allFrom(arr);
            // Adding the all the books as a batch
            for (final Book book : books) {
                db.insert(BookDB.Book.TABLE, null, book.toValues());
            }
        }

        db.setTransactionSuccessful();
        Log.i(TAG_LOG, "Successfully created " + BookDB.NAME);
    } catch (Exception e) {
        Log.e(TAG_LOG, "Error creating " + BookDB.NAME + ": ", e);
    } finally {
        db.endTransaction();
    }
}

From source file:net.smart_json_database.JSONDatabase.java

public boolean deleteTag(String name) {
    boolean returnValue = false;
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    try {//from www  .ja  v a2 s . com
        db.beginTransaction();
        returnValue = deleteTag(name, db);
        db.setTransactionSuccessful();
        notifyListenersOnTagChange(name, IDatabaseChangeListener.CHANGETYPE_DELETE);
    } catch (Exception e) {
    } finally {
        db.endTransaction();
        db.close();
    }
    return returnValue;
}

From source file:net.smart_json_database.JSONDatabase.java

public int insertTag(String name) {
    int returnValue = -1;
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    try {//from   ww w  . j  a  v a 2  s. co m
        db.beginTransaction();
        returnValue = insertTag(name, db);
        db.setTransactionSuccessful();
        notifyListenersOnTagChange(name, IDatabaseChangeListener.CHANGETYPE_INSERT);
    } catch (Exception e) {
    } finally {
        db.endTransaction();
        db.close();
    }
    return returnValue;
}

From source file:info.staticfree.android.units.UnitUsageDBHelper.java

@SuppressWarnings("unchecked")
public void loadUnitClassifications() {
    final SQLiteDatabase db = getWritableDatabase();
    final JSONObject jo = loadInitialWeights(R.raw.unit_classification);

    db.beginTransaction();/*from w ww .j av  a2  s.  c  o  m*/
    final ContentValues cv = new ContentValues();
    for (final Iterator i = jo.keys(); i.hasNext();) {
        final String unit = (String) i.next();
        final String description = jo.optString(unit);
        final String fprint = getFingerprint(unit);
        cv.put(ClassificationEntry._FACTOR_FPRINT, fprint);
        cv.put(ClassificationEntry._DESCRIPTION, description);
        db.insert(DB_CLASSIFICATION_TABLE, null, cv);
    }
    db.setTransactionSuccessful();
    db.endTransaction();
    db.close();
    Log.d(TAG, "Successfully added " + jo.length() + " classification entries.");
}

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

public void deleteAttribute(long id) {
    SQLiteDatabase db = db();
    db.beginTransaction();/*w  w w .ja va 2 s. co m*/
    try {
        Attribute attr = get(Attribute.class, id);
        String key = attr.remoteKey;
        String[] p = new String[] { String.valueOf(id) };
        db.delete(ATTRIBUTES_TABLE, DatabaseHelper.AttributeColumns.ID + "=?", p);
        db.delete(CATEGORY_ATTRIBUTE_TABLE, DatabaseHelper.CategoryAttributeColumns.ATTRIBUTE_ID + "=?", p);
        db.delete(TRANSACTION_ATTRIBUTE_TABLE, DatabaseHelper.TransactionAttributeColumns.ATTRIBUTE_ID + "=?",
                p);
        db.setTransactionSuccessful();
        writeDeleteLog(ATTRIBUTES_TABLE, key);
    } finally {
        db.endTransaction();
    }
}

From source file:com.dm.wallpaper.board.databases.Database.java

public void addWallpapers(@NonNull WallpaperJson wallpaper) {
    String query = "INSERT INTO " + TABLE_WALLPAPERS + " (" + KEY_NAME + "," + KEY_AUTHOR + "," + KEY_URL + ","
            + KEY_THUMB_URL + "," + KEY_CATEGORY + "," + KEY_ADDED_ON + ") VALUES (?,?,?,?,?,?);";
    SQLiteDatabase db = this.getWritableDatabase();
    SQLiteStatement statement = db.compileStatement(query);
    db.beginTransaction();//from   w ww.j ava2  s  .c  o m

    for (int i = 0; i < wallpaper.getWallpapers.size(); i++) {
        statement.clearBindings();
        statement.bindString(1, wallpaper.getWallpapers.get(i).name);
        statement.bindString(2, wallpaper.getWallpapers.get(i).author);
        statement.bindString(3, wallpaper.getWallpapers.get(i).url);
        statement.bindString(4,
                wallpaper.getWallpapers.get(i).thumbUrl == null ? wallpaper.getWallpapers.get(i).url
                        : wallpaper.getWallpapers.get(i).thumbUrl);
        statement.bindString(5, wallpaper.getWallpapers.get(i).category);
        statement.bindString(6, TimeHelper.getLongDateTime());
        statement.execute();
    }
    db.setTransactionSuccessful();
    db.endTransaction();
    db.close();
}

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

/**
 * Load all of the unsynced requests, and set their status to in progress.
 * @return// w w w.  j a va 2 s. c o  m
 */
private Cursor loadRequestTableContentsForUpdating() {
    SQLiteDatabase db = getWritableDatabase();
    db.beginTransaction();
    try {
        String[] unsychronizedSelectionArgs = { UNSYCHRONIZED.toString() };
        Cursor cursor = db.query(BOTH_DATABASE_TABLE_JOIN, RESULTS_COLUMNS, SYNCHRONIZED_VALUE_WHERE,
                unsychronizedSelectionArgs, null, null, requestColumn(KEY_REQUEST_ID));
        cursor.getCount(); //For some reason, accessing the cursor count before performing the update is required for the load to work. Doesn't make much sense unless it is ignoring order.
        ContentValues updatedValues = getUpdateSynchronizedContentValues(IN_PROGRESS);
        db.update(REQUEST_DATABASE_TABLE, updatedValues, SYNCHRONIZED_VALUE_WHERE, unsychronizedSelectionArgs);

        db.setTransactionSuccessful();

        return cursor;
    } catch (Throwable t) {
        throw new RuntimeException(t);
    } finally {
        db.endTransaction();
    }
}

From source file:mobisocial.bento.anyshare.util.DBHelper.java

@Override
public void onCreate(SQLiteDatabase db) {
    db.beginTransaction();/*  www  .j a  va2  s .  c o  m*/

    createTable(db, ItemObject.TABLE, null, ItemObject._ID, "INTEGER PRIMARY KEY", ItemObject.FEEDNAME, "TEXT",
            ItemObject.TITLE, "TEXT", ItemObject.DESC, "TEXT", ItemObject.TIMESTAMP, "INTEGER", ItemObject.RAW,
            "BLOB", ItemObject.OBJHASH, "INTEGER", ItemObject.PARENT_ID, "INTEGER");
    createIndex(db, "INDEX", "objects_by_hash", ItemObject.TABLE, ItemObject.OBJHASH);
    createIndex(db, "INDEX", "objects_timestamp", ItemObject.TABLE, ItemObject.TIMESTAMP);
    db.execSQL("CREATE INDEX objects_by_parent_id ON " + ItemObject.TABLE + "(" + ItemObject.PARENT_ID + ", "
            + ItemObject.TIMESTAMP + ")");

    db.setVersion(VERSION);
    db.setTransactionSuccessful();
    db.endTransaction();
    this.onOpen(db);
    //}
}

From source file:com.osfans.trime.DictionaryHelper.java

private boolean importSchema(InputStream is) {
    boolean success = false;
    SQLiteDatabase db = getWritableDatabase();
    db.beginTransaction();/*  w w w  .j  ava 2 s  .co  m*/
    try {
        Yaml yaml = new Yaml();
        Map<String, Object> y = (Map<String, Object>) (yaml.load(is));
        Map<String, Object> m = (Map<String, Object>) y.get("schema");
        String schema_id = (String) m.get("schema_id");
        String name = (String) m.get("name");
        String full = yaml.dump(y);
        is.close();

        ContentValues initialValues = new ContentValues();
        initialValues.put("schema_id", schema_id);
        initialValues.put("name", name);
        initialValues.put("full", full);

        long r = db.update("schema", initialValues, "schema_id = ?", new String[] { schema_id });
        if (r == 0)
            r = db.insert("schema", null, initialValues);
        db.setTransactionSuccessful();
        success = true;
    } catch (Exception e) {
        throw new RuntimeException("Error import schema", e);
    } finally {
        db.endTransaction();
    }
    return success;
}

From source file:net.smart_json_database.JSONDatabase.java

/**
 * Deletes all content without the meta data
 * /*ww  w  .  j a  v  a  2  s  .com*/
 * @return true if all delte operations are succesfull
 */
public boolean clearAllTables() {
    boolean returnValue = true;
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    try {
        db.beginTransaction();
        //db.execSQL("DELETE FROM " + TABLE_Meta);
        db.execSQL("DELETE FROM " + TABLE_TAG);
        db.execSQL("DELETE FROM " + TABLE_JSON_DATA);
        db.execSQL("DELETE FROM " + TABLE_REL_TAG_JSON_DATA);
        db.setTransactionSuccessful();
    } catch (Exception e) {
        returnValue = false;
    } finally {
        db.endTransaction();
        db.close();
    }
    return returnValue;
}