Example usage for android.database.sqlite SQLiteDatabase beginTransaction

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

Introduction

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

Prototype

public void beginTransaction() 

Source Link

Document

Begins a transaction in EXCLUSIVE mode.

Usage

From source file:edu.asu.bscs.csiebler.waypointdatabase.MainActivity.java

private JSONArray getJsonResults() {
    JSONArray resultSet = new JSONArray();

    try {//from   w w w.j a v  a 2  s .  c om
        WaypointDB db = new WaypointDB(this);
        db.copyDB();
        SQLiteDatabase waypointDB = db.openDB();
        waypointDB.beginTransaction();

        String searchQuery = "SELECT  * FROM waypoint";
        Cursor cursor = waypointDB.rawQuery(searchQuery, null);

        cursor.moveToFirst();

        while (cursor.isAfterLast() == false) {
            int totalColumn = cursor.getColumnCount();
            JSONObject rowObject = new JSONObject();

            for (int i = 0; i < totalColumn; i++) {
                if (cursor.getColumnName(i) != null) {
                    try {
                        if (cursor.getString(i) != null) {
                            Log.d("TAG_NAME", cursor.getString(i));
                            rowObject.put(cursor.getColumnName(i), cursor.getString(i));
                        } else {
                            rowObject.put(cursor.getColumnName(i), "");
                        }
                    } catch (Exception e) {
                        Log.d("TAG_NAME", e.getMessage());
                    }
                }
            }

            resultSet.put(rowObject);
            cursor.moveToNext();
        }

        cursor.close();
        waypointDB.endTransaction();
        waypointDB.close();
        db.close();

        Log.d("TAG_NAME", resultSet.toString());
    } catch (Exception ex) {
        Log.w(getClass().getSimpleName(), "Exception creating adapter: " + ex.getMessage());
    }

    return resultSet;
}

From source file:ru.ming13.gambit.database.DatabaseOperator.java

private void insertDatabaseContents(SQLiteDatabase database, File databaseFile) {
    database.execSQL(SqlBuilder.buildAttachingClause(databaseFile.getAbsolutePath(), DATABASE_ALIAS));

    try {//from   w w w. j av a2  s  .c  o  m
        database.beginTransaction();

        database.execSQL(SqlBuilder.buildInsertionClause(DatabaseSchema.Tables.DECKS, DATABASE_ALIAS));
        database.execSQL(SqlBuilder.buildInsertionClause(DatabaseSchema.Tables.CARDS, DATABASE_ALIAS));

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

    database.execSQL(SqlBuilder.buildDetachingClause(DATABASE_ALIAS));
}

From source file:au.org.ala.fielddata.mobile.dao.DatabaseHelper.java

@Override
public void onCreate(SQLiteDatabase db) {
    try {//from www .  ja v a  2 s.  com
        db.beginTransaction();

        for (String table : TABLES) {
            db.execSQL("CREATE TABLE " + table + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, server_id INTEGER, "
                    + "created INTEGER, updated INTEGER, last_sync INTEGER" + "name TEXT, json TEXT)");

            createServerIdIndex(db, table);
        }

        createRecordTable(db);
        createSpeciesTables(db);
        createAttributeRowTable(db);

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

}

From source file:com.hemou.android.core.sync.persistence.DatabaseCache.java

public <Key, E> void del(PersistableResource<E> persistableResource, Key... keys) {
    if (keys == null || keys.length == 0)
        return;/* w w  w  .  ja va 2 s . c om*/
    SQLiteOpenHelper helper = helperProvider.get();
    final SQLiteDatabase db = getWritable(helper);
    if (db == null)
        return;

    db.beginTransaction();
    try {
        persistableResource.del(db, keys);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        helper.close();
    }
}

From source file:com.hemou.android.core.sync.persistence.DatabaseCache.java

public <E> void add(PersistableResource<E> persistableResource, E... datas) {
    if (datas == null || datas.length == 0)
        return;/*from  w  ww  . j  a  va 2  s  . c o  m*/
    SQLiteOpenHelper helper = helperProvider.get();
    final SQLiteDatabase db = getWritable(helper);
    if (db == null)
        return;

    db.beginTransaction();
    try {
        persistableResource.add(db, datas);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        helper.close();
    }
}

From source file:syncthing.android.settings.AppSettings.java

public void setDefaultCredentials(Credentials creds) {
    SQLiteDatabase _db = db.getWritableDatabase();
    try {//  w ww .j  a v  a2s  . com
        _db.beginTransaction();
        ContentValues cv = new ContentValues();
        cv.put(CredentialsDB.SCHEMA.DEFAULT, 0);
        //first unset default for everyone
        _db.update(CredentialsDB.SCHEMA.TABLE, cv, null, null);
        cv.put(CredentialsDB.SCHEMA.DEFAULT, 1);
        String[] sel = new String[] { creds.id };
        //set default for specified device
        _db.update(CredentialsDB.SCHEMA.TABLE, cv, credentialsDeviceIdSel, sel);
        _db.setTransactionSuccessful();
    } finally {
        _db.endTransaction();
    }
}

From source file:au.org.ala.fielddata.mobile.dao.DatabaseHelper.java

public synchronized void doInTransaction(UpdateWork work) {

    SQLiteDatabase db = getWritableDatabase();
    try {/*from  www  . ja  va  2  s. co  m*/
        db.beginTransaction();

        work.doUpdate();

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

From source file:com.hemou.android.core.sync.persistence.DatabaseCache.java

private <E> List<E> requestAndStore(final SQLiteOpenHelper helper,
        final PersistableResource<E> persistableResource) throws IOException {
    List<E> items = null;/*from  w ww . j a  v  a  2s  . com*/
    try {
        items = persistableResource.request();

    } catch (ExpiredAuthorizationException e) {
        return new ArrayList<E>();
    }

    final SQLiteDatabase db = getWritable(helper);
    if (db == null)
        return items;

    db.beginTransaction();
    try {
        persistableResource.store(db, items);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
    return items;
}

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

private boolean importSchema(InputStream is) {
    boolean success = false;
    SQLiteDatabase db = getWritableDatabase();
    db.beginTransaction();
    try {/*from   ww w  .  j a v a  2 s . co  m*/
        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:syncthing.android.settings.AppSettings.java

public void removeCredentials(Credentials creds) {
    SQLiteDatabase _db = db.getWritableDatabase();
    Cursor c = null;//from w  w  w  . j a  v a 2s  .c o  m
    try {
        _db.beginTransaction();
        _db.delete(CredentialsDB.SCHEMA.TABLE, credentialsDeviceIdSel, new String[] { creds.id });
        c = _db.query(CredentialsDB.SCHEMA.TABLE, idCols, credentialsDefaultSel, null, null, null, null);
        if (c != null && c.getCount() == 0) {
            c.close();
            //no default set a new one
            c = _db.query(CredentialsDB.SCHEMA.TABLE, idCols, null, null, null, null, null);
            if (c != null && c.moveToFirst()) {
                ContentValues cv = new ContentValues();
                cv.put(CredentialsDB.SCHEMA.DEFAULT, 1);
                _db.update(CredentialsDB.SCHEMA.TABLE, cv, idSel, new String[] { c.getString(0) });
            }
        }
        _db.setTransactionSuccessful();
    } finally {
        _db.endTransaction();
        if (c != null)
            c.close();
    }
}