Example usage for android.database.sqlite SQLiteOpenHelper getWritableDatabase

List of usage examples for android.database.sqlite SQLiteOpenHelper getWritableDatabase

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteOpenHelper getWritableDatabase.

Prototype

public SQLiteDatabase getWritableDatabase() 

Source Link

Document

Create and/or open a database that will be used for reading and writing.

Usage

From source file:com.google.zxing.client.android.history.HistoryManager.java

/**
 * <p>/*  w  ww .j a  va  2 s .  c om*/
 * Builds a text representation of the scanning history. Each scan is
 * encoded on one line, terminated by a line break (\r\n). The values in
 * each line are comma-separated, and double-quoted. Double-quotes within
 * values are escaped with a sequence of two double-quotes. The fields
 * output are:
 * </p>
 * 
 * <ul>
 * <li>Raw text</li>
 * <li>Display text</li>
 * <li>Format (e.g. QR_CODE)</li>
 * <li>Timestamp</li>
 * <li>Formatted version of timestamp</li>
 * </ul>
 */
CharSequence buildHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        db = helper.getWritableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null,
                DBHelper.TIMESTAMP_COL + " DESC");

        StringBuilder historyText = new StringBuilder(1000);
        while (cursor.moveToNext()) {

            historyText.append('"').append(massageHistoryField(cursor.getString(0))).append("\",");
            historyText.append('"').append(massageHistoryField(cursor.getString(1))).append("\",");
            historyText.append('"').append(massageHistoryField(cursor.getString(2))).append("\",");
            historyText.append('"').append(massageHistoryField(cursor.getString(3))).append("\",");

            // Add timestamp again, formatted
            long timestamp = cursor.getLong(3);
            historyText.append('"')
                    .append(massageHistoryField(EXPORT_DATE_TIME_FORMAT.format(new Date(timestamp))))
                    .append("\",");

            // Above we're preserving the old ordering of columns which had
            // formatted data in position 5

            historyText.append('"').append(massageHistoryField(cursor.getString(4))).append("\"\r\n");
        }
        return historyText;
    } finally {
        close(cursor, db);
    }
}

From source file:de.vanita5.twittnuker.provider.TwidereDataProvider.java

@Override
public SQLiteDatabase onCreateSQLiteDatabase() {
    final TwittnukerApplication app = TwittnukerApplication.getInstance(getContext());
    final SQLiteOpenHelper helper = app.getSQLiteOpenHelper();
    return helper.getWritableDatabase();
}

From source file:org.getlantern.firetweet.provider.FiretweetDataProvider.java

@Override
public SQLiteDatabase onCreateSQLiteDatabase() {
    final FiretweetApplication app = FiretweetApplication.getInstance(getContext());
    final SQLiteOpenHelper helper = app.getSQLiteOpenHelper();
    return helper.getWritableDatabase();
}

From source file:mobisocial.musubi.ui.fragments.AccountLinkDialog.java

/**
 * Adds an account to the local database. Must not be called on the main thread.
 *//*from   w w w  . jav  a 2s. c o  m*/
public static MMyAccount addAccountToDatabase(Activity activity, AccountDetails accountDetails) {
    SQLiteOpenHelper databaseSource = App.getDatabaseSource(activity);
    IdentitiesManager im = new IdentitiesManager(databaseSource);
    MyAccountManager am = new MyAccountManager(databaseSource);
    DeviceManager dm = new DeviceManager(databaseSource);
    FeedManager fm = new FeedManager(databaseSource);

    String accountType = accountDetails.accountType;
    String accountName = accountDetails.accountName;
    String principal = accountDetails.principal;
    boolean owned = accountDetails.owned;
    IBIdentity ibid;
    if (accountType.equals(ACCOUNT_TYPE_GOOGLE)) {
        ibid = new IBIdentity(Authority.Email, principal, 0);
    } else if (accountType.equals(ACCOUNT_TYPE_FACEBOOK)) {
        ibid = new IBIdentity(Authority.Facebook, principal, 0);
    } else if (accountType.equals(ACCOUNT_TYPE_PHONE)) {
        ibid = new IBIdentity(Authority.PhoneNumber, principal, 0);
    } else {
        throw new RuntimeException("Unsupported account type " + accountType);
    }

    SQLiteDatabase db = databaseSource.getWritableDatabase();
    db.beginTransaction();
    try {
        // Ensure identity in the database
        MIdentity id = im.getIdentityForIBHashedIdentity(ibid);
        //don't repeatedly add profile broadcast groups or do any
        //of this processing if the account is already owned.
        if (id != null && id.owned_) {
            return null;
        }

        MIdentity original = im.getOwnedIdentities().get(0);
        //if this identity wasnt already in the contact book, we need to update it
        if (id == null) {
            id = new MIdentity();
            populateMyNewIdentity(activity, principal, im, ibid, id, original, owned);
            im.insertIdentity(id);
        } else {
            populateMyNewIdentity(activity, principal, im, ibid, id, original, owned);
            im.updateIdentity(id);
        }

        im.updateMyProfileName(activity, id.musubiName_, false);
        im.updateMyProfileThumbnail(activity, id.musubiThumbnail_, false);

        // Ensure account entry exists
        MMyAccount account = am.lookupAccount(accountName, accountType);
        if (account == null) {
            //create the account
            account = new MMyAccount();
            account.accountName_ = accountName;
            account.accountType_ = accountType;
            account.identityId_ = id.id_;
            am.insertAccount(account);
        } else {
            account.identityId_ = id.id_;
            am.updateAccount(account);
        }

        // For accounts linked to identities that are not yet owned,
        // skip further initialization
        if (owned) {
            MDevice dev = dm.getDeviceForName(id.id_, dm.getLocalDeviceName());
            // Ensure device exists
            if (dev == null) {
                dev = new MDevice();
                dev.deviceName_ = dm.getLocalDeviceName();
                dev.identityId_ = id.id_;
                dm.insertDevice(dev);
            }
            //this feed will contain all members who should receive
            //a profile for the account because of a friend introduction
            MFeed provisional = new MFeed();
            provisional.name_ = MFeed.PROVISONAL_WHITELIST_FEED_NAME;
            provisional.type_ = MFeed.FeedType.ASYMMETRIC;
            fm.insertFeed(provisional);
            //XXX
            //TODO: in other places in the code, we should be pruning the
            //provisional whitelist feed as people become whitelisted..

            //these get inserted for owned identities to allow profile
            //broadcasts to go out
            MMyAccount provAccount = new MMyAccount();
            provAccount.accountName_ = MMyAccount.PROVISIONAL_WHITELIST_ACCOUNT;
            provAccount.accountType_ = MMyAccount.INTERNAL_ACCOUNT_TYPE;
            provAccount.identityId_ = id.id_;
            provAccount.feedId_ = provisional.id_;
            am.insertAccount(provAccount);

            //this feed will contain all members who should receive
            //a profile for the account because they are whitelisted
            //and contacted you on one of your accounts.
            MFeed accountBroadcastFeed = new MFeed();
            accountBroadcastFeed.name_ = MFeed.LOCAL_WHITELIST_FEED_NAME;
            accountBroadcastFeed.type_ = MFeed.FeedType.ASYMMETRIC;
            fm.insertFeed(accountBroadcastFeed);

            MMyAccount localAccount = new MMyAccount();
            localAccount.accountName_ = MMyAccount.LOCAL_WHITELIST_ACCOUNT;
            localAccount.accountType_ = MMyAccount.INTERNAL_ACCOUNT_TYPE;
            localAccount.identityId_ = id.id_;
            localAccount.feedId_ = accountBroadcastFeed.id_;
            am.insertAccount(localAccount);

            db.setTransactionSuccessful();

            ContentResolver resolver = activity.getContentResolver();
            // Notify interested services (identity available makes AMQP wake up for example)
            resolver.notifyChange(MusubiService.OWNED_IDENTITY_AVAILABLE, null);
            resolver.notifyChange(MusubiService.MY_PROFILE_UPDATED, null);

            // Makes key update wake up
            resolver.notifyChange(MusubiService.AUTH_TOKEN_REFRESH, null);
            WizardStepHandler.accomplishTask(activity, WizardStepHandler.TASK_LINK_ACCOUNT);

            App.getUsageMetrics(activity).report(MusubiMetrics.ACCOUNT_CONNECTED, account.accountType_);
        } else {
            db.setTransactionSuccessful();
        }
        return account;
    } finally {
        db.endTransaction();
    }
}