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.flowzr.activity.MainActivity.java

private void initialLoad() {
    long t3, t2, t1, t0 = System.currentTimeMillis();
    DatabaseAdapter db = new DatabaseAdapter(this);
    db.open();/*  w  w w  . j ava 2s  . co m*/
    try {
        SQLiteDatabase x = db.db();
        x.beginTransaction();
        t1 = System.currentTimeMillis();
        try {
            updateFieldInTable(x, DatabaseHelper.CATEGORY_TABLE, 0, "title", getString(R.string.no_category));
            updateFieldInTable(x, DatabaseHelper.CATEGORY_TABLE, -1, "title", getString(R.string.split));
            updateFieldInTable(x, DatabaseHelper.PROJECT_TABLE, 0, "title", getString(R.string.no_project));
            updateFieldInTable(x, DatabaseHelper.LOCATIONS_TABLE, 0, "name",
                    getString(R.string.current_location));
            x.setTransactionSuccessful();
        } finally {
            x.endTransaction();
        }
        t2 = System.currentTimeMillis();
        if (MyPreferences.shouldUpdateHomeCurrency(this)) {
            db.setDefaultHomeCurrency();
        }
        CurrencyCache.initialize(db.em());
        t3 = System.currentTimeMillis();
        if (MyPreferences.shouldRebuildRunningBalance(this)) {
            db.rebuildRunningBalances();
        }
        if (MyPreferences.shouldUpdateAccountsLastTransactionDate(this)) {
            db.updateAccountsLastTransactionDate();
        }
    } finally {
        db.close();
    }
    long t4 = System.currentTimeMillis();
    Log.d("Financisto",
            "Load time = " + (t4 - t0) + "ms = " + (t2 - t1) + "ms+" + (t3 - t2) + "ms+" + (t4 - t3) + "ms");
}

From source file:ch.sebastienzurfluh.swissmuseumguides.contentprovider.model.io.connectors.LocalConnector.java

@Override
public void addAll(final Menus menus) {
    new Thread() {
        @Override/*  w w w. j  av a2s . c  om*/
        public void run() {
            SQLiteDatabase writableDatabase = getWritableDatabase();

            writableDatabase.beginTransaction();
            try {
                for (Menu menu : menus) {
                    String query = "INSERT OR REPLACE INTO " + menus.getTableName() + " VALUES (\""
                            + menu.getId() + "\", \"" + menu.getTitle() + "\", \"" + menu.getDescription()
                            + "\", \"" + menu.getThumbImgURL() + "\", \"" + menu.getImgURL() + "\");";
                    getWritableDatabase().execSQL(query);
                }
                writableDatabase.setTransactionSuccessful();
            } catch (JSONException e) {
                System.out.println("Malformed json in menus");
                e.printStackTrace();
            } finally {
                writableDatabase.endTransaction();
            }
        }
    }.start();
}

From source file:ru.gkpromtech.exhibition.db.Table.java

public void deleteById(List<Integer> ids) {
    if (ids.isEmpty())
        return;//  w w w . ja  va 2s  .  c  om

    SQLiteDatabase db = mSqlHelper.getWritableDatabase();
    try {
        db.beginTransaction();

        String args[] = DbHelper.makeArguments(ids.toArray(new Integer[ids.size()]));
        String params = DbHelper.makePlaceholders(args.length);
        db.delete(mTableName, "id IN (" + params + ")", args);

        db.setTransactionSuccessful();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.endTransaction();
        db.close();
    }
}

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

public void removeCredentials(Credentials creds) {
    SQLiteDatabase _db = db.getWritableDatabase();
    Cursor c = null;/*from  w  ww  .j  a  v a2 s  .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();
    }
}

From source file:ch.sebastienzurfluh.swissmuseumguides.contentprovider.model.io.connectors.LocalConnector.java

@Override
public void addAll(final Affiliations affiliations) {
    new Thread() {
        @Override/*from w w w .  java 2s .c  o  m*/
        public void run() {
            SQLiteDatabase writableDatabase = getWritableDatabase();

            writableDatabase.beginTransaction();
            try {
                for (Affiliation affiliation : affiliations) {
                    String query = "INSERT OR REPLACE INTO " + affiliations.getTableName() + " VALUES (\""
                            + affiliation.getId() + "\", \"" + affiliation.getPageId() + "\", \""
                            + affiliation.getGroupId() + "\", " + affiliation.getOrder() + ");";
                    getWritableDatabase().execSQL(query);
                }
                writableDatabase.setTransactionSuccessful();
            } catch (JSONException e) {
                System.out.println("Malformed json in affiliations");
                e.printStackTrace();
            } finally {
                writableDatabase.endTransaction();
            }
        }
    }.start();
}

From source file:ch.sebastienzurfluh.swissmuseumguides.contentprovider.model.io.connectors.LocalConnector.java

@Override
public void addAll(final Resources resources) {
    new Thread() {
        @Override/*from  w w  w. ja v a2 s . c o m*/
        public void run() {
            SQLiteDatabase writableDatabase = getWritableDatabase();

            writableDatabase.beginTransaction();
            try {
                for (Resource resource : resources) {
                    String query = "INSERT OR REPLACE INTO " + resources.getTableName() + " VALUES (\""
                            + resource.getId() + "\", \"" + resource.getTitle() + "\", \"" + resource.getURL()
                            + "\", \"" + resource.getDescription() + "\", \"" + resource.getType() + "\");";
                    getWritableDatabase().execSQL(query);
                }
                writableDatabase.setTransactionSuccessful();
            } catch (JSONException e) {
                System.out.println("Malformed json in resources");
                e.printStackTrace();
            } finally {
                writableDatabase.endTransaction();
            }
        }
    }.start();
}

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

public void saveCredentials(Credentials creds) {
    SQLiteDatabase _db = db.getWritableDatabase();
    Cursor c = null;// www  .jav a  2 s. co m
    try {
        ContentValues cv = new ContentValues();
        cv.put(CredentialsDB.SCHEMA.ALIAS, creds.alias);
        cv.put(CredentialsDB.SCHEMA.URL, creds.url);
        cv.put(CredentialsDB.SCHEMA.API_KEY, creds.apiKey);
        cv.put(CredentialsDB.SCHEMA.CERT, creds.caCert);
        String[] sel = new String[] { creds.id };
        _db.beginTransaction();
        c = _db.query(CredentialsDB.SCHEMA.TABLE, idCols, credentialsDeviceIdSel, sel, null, null, null);
        if (c != null && c.getCount() > 0) {
            _db.update(CredentialsDB.SCHEMA.TABLE, cv, credentialsDeviceIdSel, sel);
        } else {
            cv.put(CredentialsDB.SCHEMA.DEVICE_ID, creds.id);
            _db.insert(CredentialsDB.SCHEMA.TABLE, null, cv);
        }
        _db.setTransactionSuccessful();
    } finally {
        _db.endTransaction();
        if (c != null)
            c.close();
    }
}

From source file:org.kontalk.provider.UsersProvider.java

private void endTransaction(SQLiteDatabase db, boolean success) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB)
        db.endTransaction();
    else/*from  ww  w  .j  a v  a 2  s  .c  om*/
        db.execSQL(success ? "COMMIT" : "ROLLBACK");
}

From source file:com.ericsender.android_nanodegree.popmovie.com.ericsender.android_nanodegree.popmovie.data.TestProvider.java

public void testBasicFavoriteQueries() {
    //first insert movies:
    Map<Long, ContentValues> listContentValues = TestUtilities.createSortedMovieValues(getContext(), "popular");
    Map<Long, Long> locationRowIds = TestUtilities.insertMovieRow(mContext, listContentValues);

    // Insert random favorites
    SQLiteDatabase db = new MovieDbHelper(getContext()).getWritableDatabase();
    Set<Long> insertedMoviedIds = new HashSet<>();
    try {/* www  .ja v a 2  s. c om*/
        db.beginTransaction();
        while (insertedMoviedIds.isEmpty())
            for (Map.Entry<Long, ContentValues> e : listContentValues.entrySet())
                insertedMoviedIds.add(TestUtilities.generateRandomFavoritesAndInsert(db, e.getValue()));
        insertedMoviedIds.remove(null);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        db.close();
    }

    // Test the basic content provider query
    Cursor favCursor = mContext.getContentResolver().query(MovieContract.FavoriteEntry.CONTENT_URI, null, null,
            null, null);

    // Make sure we get the correct cursor out of the database
    TestUtilities.validateFavoritesCursor(favCursor, listContentValues, insertedMoviedIds);

    // Has the NotificationUri been set correctly? --- we can only test this easily against API
    // level 19 or greater because getNotificationUri was added in API level 19.
    if (Build.VERSION.SDK_INT >= 19) {
        assertEquals("Error: Favoriate Query did not properly set NotificationUri",
                favCursor.getNotificationUri(), MovieContract.FavoriteEntry.buildUri());
    }
}

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

/**
 * Set whether notifications are enabled for a password file
 *///from  ww w. j a  v  a  2 s  .c  o  m
public void setPasswdExpiryNotif(@NonNull PasswdFileData fileData, boolean enabled) {
    try {
        SQLiteDatabase db = itsDbHelper.getWritableDatabase();
        try {
            db.beginTransaction();
            Long uriId = getDbUriId(fileData.getUri(), db);
            if (enabled) {
                if (uriId == null) {
                    enablePasswdExpiryNotif(fileData, db);
                }
            } else {
                if (uriId != null) {
                    removeUri(uriId, db);
                    loadEntries(db);
                }
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } catch (SQLException e) {
        Log.e(TAG, "Database error", e);
    }
}