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:Main.java

private static void executeStatements(SQLiteDatabase database, List<String> statements) {
    try {/*from   w ww  . ja  v a 2 s  .  co  m*/
        database.beginTransaction();
        for (String statement : statements) {
            database.execSQL(statement);
        }
        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }
}

From source file:Main.java

public static int executeSqlStatementsInTx(SQLiteDatabase db, String[] statements) {
    db.beginTransaction();//from   ww  w.  ja v a 2s .  c  o m
    try {
        int count = executeSqlStatements(db, statements);
        db.setTransactionSuccessful();
        return count;
    } finally {
        db.endTransaction();
    }
}

From source file:Main.java

public static final int setBulkInsert(Context context, String table, SQLiteDatabase myDb,
        ContentValues[] values) {/*from w ww . j  a  va  2s .  c o  m*/

    int returnCount = 0;

    try {

        for (ContentValues value : values) {

            final long _id = myDb.replace(table, null, value);

            if (_id != -1)
                returnCount++;
        }

        myDb.setTransactionSuccessful();

    } finally {
        myDb.endTransaction();
    }

    return returnCount;
}

From source file:org.droidparts.inner.PersistUtils.java

public static <Result> Result executeInTransaction(SQLiteDatabase db, Callable<Result> task) {
    db.beginTransaction();//ww  w. j a  v  a  2s. c o  m
    try {
        Result result = task.call();
        db.setTransactionSuccessful();
        return result;
    } catch (Exception e) {
        L.w(e.getMessage());
        L.d(e);
        return null;
    } finally {
        db.endTransaction();
    }
}

From source file:curso.android.DAO.RespostaDAO.java

private static void createTable(SQLiteDatabase database, String tableName) {
    try {//from  w w w . jav a2  s  . c om
        database.beginTransaction();

        String tableSql = "CREATE TABLE IF NOT EXISTS " + tableName + " ("
                + "asw_id INTEGER PRIMARY KEY AUTOINCREMENT," + "ask_id INTEGER," + "user_id INTEGER,"
                + "asw_resposta TEXT," + "user_name TEXT);";
        database.execSQL(tableSql);

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

From source file:curso.android.DAO.PerguntaDAO.java

private static void createTable(SQLiteDatabase database, String tableName) {
    try {/*  w w  w  .  ja va 2  s  .  c o m*/
        // begin the transaction
        database.beginTransaction();

        // Create a table
        String tableSql = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + "ask_id INTEGER PRIMARY KEY,"
                + "user_id INTEGER," + "ask_pergunta TEXT," + "user_name TEXT," + "status INTEGER);";
        database.execSQL(tableSql);

        // this makes sure transaction is committed
        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }
}

From source file:com.contentful.vault.SqliteHelper.java

static void clearRecords(SpaceHelper helper, SQLiteDatabase db) {
    db.beginTransaction();//from  w ww. ja va2s  . c o m
    try {
        db.delete(escape(TABLE_ENTRY_TYPES), null, null);
        db.delete(escape(TABLE_SYNC_INFO), null, null);
        for (String code : helper.getLocales()) {
            db.delete(escape(localizeName(TABLE_ASSETS, code)), null, null);
            db.delete(escape(localizeName(TABLE_LINKS, code)), null, null);

            for (ModelHelper<?> modelHelper : helper.getModels().values()) {
                db.delete(escape(localizeName(modelHelper.getTableName(), code)), null, null);
            }
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}

From source file:org.thinschema.dataaccess.JSONAdapter.java

/**
 * Fill a table with data from a JSON. The JSON must contain an array of
 * JSON named 'rows', where each JSON represents one row (or record)
 * in the database.//from   w ww . ja v  a 2  s .  c  o m
 *
 * @param sqLiteDatabase SQLiteDatabase instance.
 * @param dbSchema       Database schema.
 * @param tableName      Table name.
 * @param jsonData       JSONObject
 * @return true if insertion is successful.
 */
public static boolean fill(SQLiteDatabase sqLiteDatabase, DBSchema dbSchema, String tableName,
        JSONObject jsonData) {
    boolean success = true;
    JSONArray data = jsonData.optJSONArray("rows");

    sqLiteDatabase.beginTransaction();
    try {
        if (data != null && data.length() > 0) {
            for (int i = 0, size = data.length(); i < size; ++i) {
                JSONObject row = data.optJSONObject(i);
                ContentValues cv = toContentValues(row, dbSchema, tableName);
                if (sqLiteDatabase.insert(tableName, null, cv) == -1) {
                    success = false;
                    break;
                }
            }
        }

        if (success) {
            sqLiteDatabase.setTransactionSuccessful();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sqLiteDatabase.endTransaction();
    }
    return success;
}

From source file:com.contentful.vault.SqliteHelper.java

static void deleteTables(SQLiteDatabase db) {
    String[] columns = new String[] { "name" };
    String selection = "type = ? AND name != ?";
    String[] args = new String[] { "table", "android_metadata" };
    Cursor cursor = db.query("sqlite_master", columns, selection, args, null, null, null);
    List<String> tables = null;
    try {/* w ww  . ja  v a 2  s  .c  om*/
        if (cursor.moveToFirst()) {
            tables = new ArrayList<>();
            do {
                tables.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
    } finally {
        cursor.close();
    }
    if (tables != null) {
        db.beginTransaction();
        try {
            for (String table : tables) {
                db.execSQL("DROP TABLE " + escape(table));
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
}

From source file:edu.htl3r.schoolplanner.backend.database.AutoSelectDatabase.java

private void deleteAllRowsFromDatabaseTransaction(String table, String loginSetKey) {
    SQLiteDatabase database = this.database.openDatabase(true);
    database.beginTransaction();/*from w ww .java2  s  . c  o m*/

    this.database.deleteAllRowsWithLoginSetKey(database, table, loginSetKey);

    database.setTransactionSuccessful();
    database.endTransaction();
    this.database.closeDatabase(database);
}