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:net.smart_json_database.JSONDatabase.java

public boolean delete(JSONEntity entity) {
    boolean returnValue = false;
    if (entity.getUid() == -1) {
        return returnValue;
    }//from   w  w  w.  j ava2 s.  c o m
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    try {
        db.beginTransaction();
        String[] params = new String[] { "" + entity.getUid() };
        db.delete(TABLE_REL_JSON_DATA_JSON_DATA, "from_id = ?", params);
        db.delete(TABLE_REL_JSON_DATA_JSON_DATA, "to_id = ?", params);
        db.delete(TABLE_REL_TAG_JSON_DATA, "to_id = ?", params);
        db.delete(TABLE_JSON_DATA, "json_uid = ?", params);
        db.setTransactionSuccessful();
        notifyListenersOnEntityChange(entity.getUid(), IDatabaseChangeListener.CHANGETYPE_DELETE);
        returnValue = true;
    } catch (Exception e) {
        returnValue = false;
    } finally {
        db.endTransaction();
        db.close();
    }

    return returnValue;
}

From source file:org.path.episample.android.tasks.InitializationTask.java

private void registerCensusForm() {
    // create a census table in ODK Survey's database
    SQLiteDatabase db = null;

    try {//  ww w .  j  ava 2s .  co  m
        // db = DatabaseFactory.getInstanceDatabase(context, appName);
        db = DatabaseFactory.get().getDatabase(getApplication(), getAppName());

        List<Column> columns = CensusColumns.USER_DEFINED_COLUMNS;

        ArrayList<ColumnDefinition> orderedDefs = ColumnDefinition.buildColumnDefinitions(appName,
                CensusDatabaseHelper.CENSUS_DATABASES_TABLE, columns);
        db.beginTransaction();
        ODKDatabaseUtils.get().createDBTableWithColumns(db, appName,
                CensusDatabaseHelper.CENSUS_DATABASES_TABLE, orderedDefs);
        db.setTransactionSuccessful();

        registerCensusFormInstanceName(db);

    } catch (Exception ex) {

    } finally {

        if (db != null) {
            db.endTransaction();
            db.close();
        }
    }
}

From source file:de.stadtrallye.rallyesoft.model.map.MapManager.java

private void updateDatabase(List<Node> nodes, List<Edge> edges) {
    SQLiteDatabase db = getDb();

    db.beginTransaction();/*from w  w w  .  java2s .c  o  m*/
    try {
        db.delete(Edges.TABLE, null, null);
        db.delete(Nodes.TABLE, null, null);

        SQLiteStatement nodeIn = db.compileStatement("INSERT INTO " + Nodes.TABLE + " ("
                + DatabaseHelper.strStr(Nodes.COLS) + ") VALUES (?, ?, ?, ?, ?)");

        SQLiteStatement edgeIn = db.compileStatement(
                "INSERT INTO " + Edges.TABLE + " (" + DatabaseHelper.strStr(Edges.COLS) + ") VALUES (?, ?, ?)");

        for (Node n : nodes) {
            nodeIn.bindLong(1, n.nodeID);
            nodeIn.bindString(2, n.name);
            nodeIn.bindDouble(3, n.location.latitude);
            nodeIn.bindDouble(4, n.location.longitude);
            nodeIn.bindString(5, n.description);
            nodeIn.executeInsert();
        }

        for (Edge m : edges) {
            edgeIn.bindLong(1, m.nodeA.nodeID);
            edgeIn.bindLong(2, m.nodeB.nodeID);
            edgeIn.bindString(3, m.type.toString());
            edgeIn.executeInsert();
        }

        db.setTransactionSuccessful();
    } catch (Exception e) {
        Log.e(THIS, "Map Update on Database failed", e);
    } finally {
        db.endTransaction();
    }
}

From source file:org.nuxeo.android.cache.sql.DeferedUpdateTableWrapper.java

public OperationRequest storeRequest(String key, OperationRequest request, OperationType opType) {

    SQLiteDatabase db = getWritableDatabase();

    String sql = "INSERT INTO " + getTableName() + " (" + KEY_COLUMN + "," + OPID_COLUMN + "," + OPTYPE_COLUMN
            + "," + PARAMS_COLUMN + "," + HEADERS_COLUMN + "," + CTX_COLUMN + "," + DEPS_COLUMN;

    String operationId = request.getDocumentation().getId();
    String jsonParams = new JSONObject(request.getParameters()).toString();
    String jsonHeaders = new JSONObject(request.getHeaders()).toString();
    String jsonCtx = new JSONObject(request.getContextParameters()).toString();
    String deps = request.getDependencies().asJSON();

    String sqlValues = " VALUES (" + "'" + key + "'," + "'" + operationId + "'," + "'" + opType.toString()
            + "'," + "'" + jsonParams + "'," + "'" + jsonHeaders + "'," + "'" + jsonCtx + "'," + "'" + deps
            + "'";

    if (request.getInput() != null) {
        String inputType = request.getInput().getInputType();
        String inputRef = request.getInput().getInputRef();
        String inputBin = new Boolean(request.getInput().isBinary()).toString();

        sql = sql + "," + INPUT_TYPE_COLUMN + "," + INPUT_REF_COLUMN + "," + INPUT_BINARY_COLUMN;
        sqlValues = sqlValues + ",'" + inputType + "','" + inputRef + "','" + inputBin + "'";
    }/*from   w  w  w .ja  v a  2s. c  o  m*/
    String insertQuery = sql + " ) " + sqlValues + ");";
    db.beginTransaction();
    db.execSQL(insertQuery);
    db.setTransactionSuccessful();
    db.endTransaction();

    return request;
}

From source file:org.liberty.android.fantastischmemopro.DatabaseHelper.java

public static void createEmptyDatabase(String path, String name) throws IOException, SQLException {
    File dbfile = new File(path + "/" + name);
    if (dbfile.exists()) {
        // throw new IOException("DB already exist");
        /* Create a backup and overwrite  it instead poping up an error */
        File backupFile = new File(dbfile.getAbsolutePath().replaceAll(".db$", ".old.db"));
        if (backupFile.exists()) {
            backupFile.delete();//from ww  w .j  av  a  2 s. c  o m
        }
        dbfile.renameTo(backupFile);
    }
    SQLiteDatabase database = SQLiteDatabase.openDatabase(path + "/" + name, null,
            SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY);

    database.execSQL(
            "CREATE TABLE dict_tbl(_id INTEGER PRIMARY KEY ASC AUTOINCREMENT, question TEXT, answer TEXT, note TEXT, category TEXT)");
    database.execSQL(
            "CREATE TABLE learn_tbl(_id INTEGER PRIMARY KEY ASC AUTOINCREMENT, date_learn, interval, grade INTEGER, easiness REAL, acq_reps INTEGER, ret_reps INTEGER, lapses INTEGER, acq_reps_since_lapse INTEGER, ret_reps_since_lapse INTEGER)");
    database.execSQL("CREATE TABLE control_tbl(ctrl_key TEXT, value TEXT)");

    database.beginTransaction();
    try {
        database.execSQL("DELETE FROM learn_tbl");
        database.execSQL("INSERT INTO learn_tbl(_id) SELECT _id FROM dict_tbl");
        database.execSQL(
                "UPDATE learn_tbl SET date_learn = '2010-01-01', interval = 0, grade = 0, easiness = 2.5, acq_reps = 0, ret_reps  = 0, lapses = 0, acq_reps_since_lapse = 0, ret_reps_since_lapse = 0");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('question_locale', 'US')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('answer_locale', 'US')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('question_align', 'center')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('answer_align', 'center')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('question_font_size', '24')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('answer_font_size', '24')");
        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
        database.close();
    }

}

From source file:com.hichinaschool.flashcards.libanki.Collection.java

public synchronized void close(boolean save) {
    if (mDb != null) {
        if (!mConf.optBoolean("newBury", false)) {
            boolean mod = mDb.getMod();
            mSched.unburyCards();/*w ww  . j ava2  s  . c o m*/
            mDb.setMod(mod);
        }
        try {
            SQLiteDatabase db = getDb().getDatabase();
            if (save) {
                db.beginTransaction();
                try {
                    save();
                    db.setTransactionSuccessful();
                } finally {
                    db.endTransaction();
                }
            } else {
                if (db.inTransaction()) {
                    db.endTransaction();
                }
                lock();
            }
        } catch (RuntimeException e) {
            AnkiDroidApp.saveExceptionReportFile(e, "closeDB");
        }
        AnkiDatabaseManager.closeDatabase(mPath);
        mDb = null;
        mMedia.close();
        // Log.i(AnkiDroidApp.TAG, "Collection closed");
    }
}

From source file:com.ichi2.libanki.Collection.java

public synchronized void close(boolean save) {
    if (mDb != null) {
        if (!mConf.optBoolean("newBury", false)) {
            boolean mod = mDb.getMod();
            mSched.unburyCards();/*from w  ww  .j a  v a2  s . c  o m*/
            mDb.setMod(mod);
        }
        try {
            SQLiteDatabase db = getDb().getDatabase();
            if (save) {
                db.beginTransaction();
                try {
                    save();
                    db.setTransactionSuccessful();
                } finally {
                    db.endTransaction();
                }
            } else {
                if (db.inTransaction()) {
                    db.endTransaction();
                }
                lock();
            }
        } catch (RuntimeException e) {
            AnkiDroidApp.sendExceptionReport(e, "closeDB");
        }
        AnkiDatabaseManager.closeDatabase(mPath);
        mDb = null;
        mMedia.close();
        _closeLog();
        Timber.i("Collection closed");
    }
}

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

public void deleteTransaction(long id) {
    SQLiteDatabase db = db();
    db.beginTransaction();/*w  w w. j  av  a  2  s.  co  m*/
    try {
        deleteTransactionNoDbTransaction(id);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}

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

public void replaceRate(ExchangeRate rate, long originalDate) {
    SQLiteDatabase db = db();
    db.beginTransaction();/*from  w w w  .  ja  v a2 s .c  o m*/
    try {
        replaceRateInTransaction(rate, originalDate, db);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}

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

public void deleteRate(long fromCurrencyId, long toCurrencyId, long date) {
    SQLiteDatabase db = db();
    db.beginTransaction();//from   ww w  .j a v a  2 s.c  o  m
    try {
        deleteRateInTransaction(fromCurrencyId, toCurrencyId, date, db);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}