Example usage for android.database.sqlite SQLiteDatabase replaceOrThrow

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

Introduction

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

Prototype

public long replaceOrThrow(String table, String nullColumnHack, ContentValues initialValues)
        throws SQLException 

Source Link

Document

Convenience method for replacing a row in the database.

Usage

From source file:com.android.quicksearchbox.ShortcutRepositoryImplLog.java

@VisibleForTesting
void reportClickAtTime(SuggestionCursor suggestion, int position, long now) {
    suggestion.moveTo(position);/*from  ww w.  j a  va 2s  . c  om*/
    if (DBG) {
        Log.d(TAG, "logClicked(" + suggestion + ")");
    }

    if (SearchManager.SUGGEST_NEVER_MAKE_SHORTCUT.equals(suggestion.getShortcutId())) {
        if (DBG)
            Log.d(TAG, "clicked suggestion requested not to be shortcuted");
        return;
    }

    Corpus corpus = mCorpora.getCorpusForSource(suggestion.getSuggestionSource());
    if (corpus == null) {
        Log.w(TAG, "no corpus for clicked suggestion");
        return;
    }

    // Once the user has clicked on a shortcut, don't bother refreshing
    // (especially if this is a new shortcut)
    mRefresher.markShortcutRefreshed(suggestion.getSuggestionSource(), suggestion.getShortcutId());

    // Add or update suggestion info
    // Since intent_key is the primary key, any existing
    // suggestion with the same source+data+action will be replaced
    final ContentValues shortcut = makeShortcutRow(suggestion);
    String intentKey = shortcut.getAsString(Shortcuts.intent_key.name());

    // Log click for shortcut
    final ContentValues click = new ContentValues();
    click.put(ClickLog.intent_key.name(), intentKey);
    click.put(ClickLog.query.name(), suggestion.getUserQuery());
    click.put(ClickLog.hit_time.name(), now);
    click.put(ClickLog.corpus.name(), corpus.getName());

    runTransactionAsync(new SQLiteTransaction() {
        @Override
        protected boolean performTransaction(SQLiteDatabase db) {
            if (DBG)
                Log.d(TAG, "Adding shortcut: " + shortcut);
            db.replaceOrThrow(Shortcuts.TABLE_NAME, null, shortcut);
            db.insertOrThrow(ClickLog.TABLE_NAME, null, click);
            return true;
        }
    });
}

From source file:com.triarc.sync.SyncAdapter.java

private void addOrUpdate(SQLiteDatabase db, JsonObject entity, SyncType type)
        throws JSONException, ParseException, IOException {
    ContentValues contentValues = new ContentValues();
    for (SyncField syncField : type.getFields()) {
        this.addContentValueFor(contentValues, entity, syncField);
    }// w  ww .  j av  a2s .c  o m
    contentValues.put("__state", UNCHANGED);

    contentValues.put("__internalTimestamp", entity.get("timestamp").getAsLong());
    String localTableName = type.getName();
    db.replaceOrThrow(localTableName, null, contentValues);
}

From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java

private void createDBTableMetadata(SQLiteDatabase db, String tableId) {
    if (tableId == null || tableId.length() <= 0) {
        throw new IllegalArgumentException(t + ": application name and table name must be specified");
    }//from   www .j ava 2  s .c  o m

    // Add the table id into table definitions
    ContentValues cvTableDef = new ContentValues();
    cvTableDef.put(TableDefinitionsColumns.TABLE_ID, tableId);
    cvTableDef.putNull(TableDefinitionsColumns.SCHEMA_ETAG);
    cvTableDef.putNull(TableDefinitionsColumns.LAST_DATA_ETAG);
    cvTableDef.put(TableDefinitionsColumns.LAST_SYNC_TIME, -1);

    db.replaceOrThrow(DatabaseConstants.TABLE_DEFS_TABLE_NAME, null, cvTableDef);

    // Add the tables values into KVS
    ArrayList<ContentValues> cvTableValKVS = new ArrayList<ContentValues>();

    ContentValues cvTableVal = null;

    cvTableVal = new ContentValues();
    cvTableVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvTableVal.put(KeyValueStoreColumns.PARTITION, KeyValueStoreConstants.PARTITION_TABLE);
    cvTableVal.put(KeyValueStoreColumns.ASPECT, KeyValueStoreConstants.ASPECT_DEFAULT);
    cvTableVal.put(KeyValueStoreColumns.KEY, KeyValueStoreConstants.TABLE_COL_ORDER);
    cvTableVal.put(KeyValueStoreColumns.VALUE_TYPE, "array");
    cvTableVal.put(KeyValueStoreColumns.VALUE, "[]");
    cvTableValKVS.add(cvTableVal);

    cvTableVal = new ContentValues();
    cvTableVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvTableVal.put(KeyValueStoreColumns.PARTITION, KeyValueStoreConstants.PARTITION_TABLE);
    cvTableVal.put(KeyValueStoreColumns.ASPECT, KeyValueStoreConstants.ASPECT_DEFAULT);
    cvTableVal.put(KeyValueStoreColumns.KEY, "defaultViewType");
    cvTableVal.put(KeyValueStoreColumns.VALUE_TYPE, "string");
    cvTableVal.put(KeyValueStoreColumns.VALUE, "SPREADSHEET");
    cvTableValKVS.add(cvTableVal);

    cvTableVal = new ContentValues();
    cvTableVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvTableVal.put(KeyValueStoreColumns.PARTITION, KeyValueStoreConstants.PARTITION_TABLE);
    cvTableVal.put(KeyValueStoreColumns.ASPECT, KeyValueStoreConstants.ASPECT_DEFAULT);
    cvTableVal.put(KeyValueStoreColumns.KEY, KeyValueStoreConstants.TABLE_DISPLAY_NAME);
    cvTableVal.put(KeyValueStoreColumns.VALUE_TYPE, "object");
    cvTableVal.put(KeyValueStoreColumns.VALUE, "\"" + tableId + "\"");
    cvTableValKVS.add(cvTableVal);

    cvTableVal = new ContentValues();
    cvTableVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvTableVal.put(KeyValueStoreColumns.PARTITION, KeyValueStoreConstants.PARTITION_TABLE);
    cvTableVal.put(KeyValueStoreColumns.ASPECT, KeyValueStoreConstants.ASPECT_DEFAULT);
    cvTableVal.put(KeyValueStoreColumns.KEY, KeyValueStoreConstants.TABLE_GROUP_BY_COLS);
    cvTableVal.put(KeyValueStoreColumns.VALUE_TYPE, "array");
    cvTableVal.put(KeyValueStoreColumns.VALUE, "[]");
    cvTableValKVS.add(cvTableVal);

    cvTableVal = new ContentValues();
    cvTableVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvTableVal.put(KeyValueStoreColumns.PARTITION, KeyValueStoreConstants.PARTITION_TABLE);
    cvTableVal.put(KeyValueStoreColumns.ASPECT, KeyValueStoreConstants.ASPECT_DEFAULT);
    cvTableVal.put(KeyValueStoreColumns.KEY, KeyValueStoreConstants.TABLE_INDEX_COL);
    cvTableVal.put(KeyValueStoreColumns.VALUE_TYPE, "string");
    cvTableVal.put(KeyValueStoreColumns.VALUE, "");
    cvTableValKVS.add(cvTableVal);

    cvTableVal = new ContentValues();
    cvTableVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvTableVal.put(KeyValueStoreColumns.PARTITION, KeyValueStoreConstants.PARTITION_TABLE);
    cvTableVal.put(KeyValueStoreColumns.ASPECT, KeyValueStoreConstants.ASPECT_DEFAULT);
    cvTableVal.put(KeyValueStoreColumns.KEY, KeyValueStoreConstants.TABLE_SORT_COL);
    cvTableVal.put(KeyValueStoreColumns.VALUE_TYPE, "string");
    cvTableVal.put(KeyValueStoreColumns.VALUE, "");
    cvTableValKVS.add(cvTableVal);

    cvTableVal = new ContentValues();
    cvTableVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvTableVal.put(KeyValueStoreColumns.PARTITION, KeyValueStoreConstants.PARTITION_TABLE);
    cvTableVal.put(KeyValueStoreColumns.ASPECT, KeyValueStoreConstants.ASPECT_DEFAULT);
    cvTableVal.put(KeyValueStoreColumns.KEY, KeyValueStoreConstants.TABLE_SORT_ORDER);
    cvTableVal.put(KeyValueStoreColumns.VALUE_TYPE, "string");
    cvTableVal.put(KeyValueStoreColumns.VALUE, "");
    cvTableValKVS.add(cvTableVal);

    cvTableVal = new ContentValues();
    cvTableVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvTableVal.put(KeyValueStoreColumns.PARTITION, "TableColorRuleGroup");
    cvTableVal.put(KeyValueStoreColumns.ASPECT, KeyValueStoreConstants.ASPECT_DEFAULT);
    cvTableVal.put(KeyValueStoreColumns.KEY, "StatusColumn.ruleList");
    cvTableVal.put(KeyValueStoreColumns.VALUE_TYPE, "object");
    try {
        List<ColorRule> rules = ColorRuleUtil.getDefaultSyncStateColorRules();
        List<TreeMap<String, Object>> jsonableList = new ArrayList<TreeMap<String, Object>>();
        for (ColorRule rule : rules) {
            jsonableList.add(rule.getJsonRepresentation());
        }
        String value = ODKFileUtils.mapper.writeValueAsString(jsonableList);
        cvTableVal.put(KeyValueStoreColumns.VALUE, value);
        cvTableValKVS.add(cvTableVal);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }

    // Now add Tables values into KVS
    for (int i = 0; i < cvTableValKVS.size(); i++) {
        db.replaceOrThrow(DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME, null, cvTableValKVS.get(i));
    }
}

From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java

private void createNewColumnMetadata(SQLiteDatabase db, String tableId, ColumnDefinition column) {
    String colName = column.getElementKey();
    ArrayList<ContentValues> cvColValKVS = new ArrayList<ContentValues>();

    ContentValues cvColVal;/*from ww w.j a  v a2  s . co m*/

    cvColVal = new ContentValues();
    cvColVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvColVal.put(KeyValueStoreColumns.PARTITION, KeyValueStoreConstants.PARTITION_COLUMN);
    cvColVal.put(KeyValueStoreColumns.ASPECT, colName);
    cvColVal.put(KeyValueStoreColumns.KEY, KeyValueStoreConstants.COLUMN_DISPLAY_CHOICES_LIST);
    cvColVal.put(KeyValueStoreColumns.VALUE_TYPE, ElementDataType.array.name());
    cvColVal.put(KeyValueStoreColumns.VALUE, "[]");
    cvColValKVS.add(cvColVal);

    cvColVal = new ContentValues();
    cvColVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvColVal.put(KeyValueStoreColumns.PARTITION, KeyValueStoreConstants.PARTITION_COLUMN);
    cvColVal.put(KeyValueStoreColumns.ASPECT, colName);
    cvColVal.put(KeyValueStoreColumns.KEY, KeyValueStoreConstants.COLUMN_DISPLAY_FORMAT);
    cvColVal.put(KeyValueStoreColumns.VALUE_TYPE, ElementDataType.string.name());
    cvColVal.put(KeyValueStoreColumns.VALUE, "");
    cvColValKVS.add(cvColVal);

    cvColVal = new ContentValues();
    cvColVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvColVal.put(KeyValueStoreColumns.PARTITION, KeyValueStoreConstants.PARTITION_COLUMN);
    cvColVal.put(KeyValueStoreColumns.ASPECT, colName);
    cvColVal.put(KeyValueStoreColumns.KEY, KeyValueStoreConstants.COLUMN_DISPLAY_NAME);
    cvColVal.put(KeyValueStoreColumns.VALUE_TYPE, ElementDataType.object.name());
    String colDisplayName = "\"" + colName + "\"";
    cvColVal.put(KeyValueStoreColumns.VALUE, colDisplayName);
    cvColValKVS.add(cvColVal);

    // TODO: change bool to be integer valued in the KVS?
    cvColVal = new ContentValues();
    cvColVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvColVal.put(KeyValueStoreColumns.PARTITION, KeyValueStoreConstants.PARTITION_COLUMN);
    cvColVal.put(KeyValueStoreColumns.ASPECT, colName);
    cvColVal.put(KeyValueStoreColumns.KEY, KeyValueStoreConstants.COLUMN_DISPLAY_VISIBLE);
    cvColVal.put(KeyValueStoreColumns.VALUE_TYPE, ElementDataType.bool.name());
    cvColVal.put(KeyValueStoreColumns.VALUE, column.isUnitOfRetention() ? "true" : "false");
    cvColValKVS.add(cvColVal);

    cvColVal = new ContentValues();
    cvColVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvColVal.put(KeyValueStoreColumns.PARTITION, KeyValueStoreConstants.PARTITION_COLUMN);
    cvColVal.put(KeyValueStoreColumns.ASPECT, colName);
    cvColVal.put(KeyValueStoreColumns.KEY, KeyValueStoreConstants.COLUMN_JOINS);
    cvColVal.put(KeyValueStoreColumns.VALUE_TYPE, ElementDataType.object.name());
    cvColVal.put(KeyValueStoreColumns.VALUE, "");
    cvColValKVS.add(cvColVal);

    // Now add all this data into the database
    for (int i = 0; i < cvColValKVS.size(); i++) {
        db.replaceOrThrow(DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME, null, cvColValKVS.get(i));
    }

    // Create column definition
    ContentValues cvColDefVal = null;

    cvColDefVal = new ContentValues();
    cvColDefVal.put(ColumnDefinitionsColumns.TABLE_ID, tableId);
    cvColDefVal.put(ColumnDefinitionsColumns.ELEMENT_KEY, colName);
    cvColDefVal.put(ColumnDefinitionsColumns.ELEMENT_NAME, column.getElementName());
    cvColDefVal.put(ColumnDefinitionsColumns.ELEMENT_TYPE, column.getElementType());
    cvColDefVal.put(ColumnDefinitionsColumns.LIST_CHILD_ELEMENT_KEYS, column.getListChildElementKeys());

    // Now add this data into the database
    db.replaceOrThrow(DatabaseConstants.COLUMN_DEFINITIONS_TABLE_NAME, null, cvColDefVal);
}

From source file:org.opendatakit.common.android.utilities.ODKDatabaseUtils.java

private void createDBTableWithColumns(SQLiteDatabase db, String appName, String tableId,
        List<ColumnDefinition> orderedDefs) {
    if (tableId == null || tableId.length() <= 0) {
        throw new IllegalArgumentException(t + ": application name and table name must be specified");
    }// w ww  .j a  v a2  s.  co m

    String createTableCmd = getUserDefinedTableCreationStatement(tableId);

    StringBuilder createTableCmdWithCols = new StringBuilder();
    createTableCmdWithCols.append(createTableCmd);

    for (ColumnDefinition column : orderedDefs) {
        if (!column.isUnitOfRetention()) {
            continue;
        }
        ElementType elementType = column.getType();

        ElementDataType dataType = elementType.getDataType();
        String dbType;
        if (dataType == ElementDataType.array) {
            dbType = "TEXT";
        } else if (dataType == ElementDataType.bool) {
            dbType = "INTEGER";
        } else if (dataType == ElementDataType.configpath) {
            dbType = "TEXT";
        } else if (dataType == ElementDataType.integer) {
            dbType = "INTEGER";
        } else if (dataType == ElementDataType.number) {
            dbType = "REAL";
        } else if (dataType == ElementDataType.object) {
            dbType = "TEXT";
        } else if (dataType == ElementDataType.rowpath) {
            dbType = "TEXT";
        } else if (dataType == ElementDataType.string) {
            dbType = "TEXT";
        } else {
            throw new IllegalStateException("unexpected ElementDataType: " + dataType.name());
        }
        //@formatter:off
        createTableCmdWithCols.append(", ").append(column.getElementKey()).append(" ").append(dbType)
                .append(" NULL");
        //@formatter:on
    }

    createTableCmdWithCols.append(");");

    db.execSQL(createTableCmdWithCols.toString());

    // Create the metadata for the table - table def and KVS
    createDBTableMetadata(db, tableId);

    // Now need to call the function to write out all the column values
    for (ColumnDefinition column : orderedDefs) {
        createNewColumnMetadata(db, tableId, column);
    }

    // Need to address column order
    ContentValues cvTableVal = new ContentValues();
    cvTableVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvTableVal.put(KeyValueStoreColumns.PARTITION, KeyValueStoreConstants.PARTITION_TABLE);
    cvTableVal.put(KeyValueStoreColumns.ASPECT, KeyValueStoreConstants.ASPECT_DEFAULT);
    cvTableVal.put(KeyValueStoreColumns.KEY, KeyValueStoreConstants.TABLE_COL_ORDER);
    cvTableVal.put(KeyValueStoreColumns.VALUE_TYPE, "array");

    StringBuilder tableDefCol = new StringBuilder();

    boolean needsComma = false;
    for (ColumnDefinition def : orderedDefs) {
        if (!def.isUnitOfRetention()) {
            continue;
        }
        if (needsComma) {
            tableDefCol.append(",");
        }
        needsComma = true;
        tableDefCol.append("\"").append(def.getElementKey()).append("\"");
    }

    WebLogger.getLogger(appName).i(t, "Column order for table " + tableId + " is " + tableDefCol.toString());
    String colOrderVal = "[" + tableDefCol.toString() + "]";
    cvTableVal.put(KeyValueStoreColumns.VALUE, colOrderVal);

    // Now add Tables values into KVS
    db.replaceOrThrow(DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME, null, cvTableVal);
}

From source file:org.path.common.android.utilities.ODKDatabaseUtils.java

public void createDBTableWithColumns(SQLiteDatabase db, String appName, String tableId,
        List<ColumnDefinition> orderedDefs) {
    if (tableId == null || tableId.length() <= 0) {
        throw new IllegalArgumentException(t + ": application name and table name must be specified");
    }//from w  w w . j av  a2s . c  o m

    String createTableCmd = getUserDefinedTableCreationStatement(tableId);

    StringBuilder createTableCmdWithCols = new StringBuilder();
    createTableCmdWithCols.append(createTableCmd);

    for (ColumnDefinition column : orderedDefs) {
        if (!column.isUnitOfRetention()) {
            continue;
        }
        ElementType elementType = column.getType();

        ElementDataType dataType = elementType.getDataType();
        String dbType;
        if (dataType == ElementDataType.array) {
            dbType = "TEXT";
        } else if (dataType == ElementDataType.bool) {
            dbType = "INTEGER";
        } else if (dataType == ElementDataType.configpath) {
            dbType = "TEXT";
        } else if (dataType == ElementDataType.integer) {
            dbType = "INTEGER";
        } else if (dataType == ElementDataType.number) {
            dbType = "REAL";
        } else if (dataType == ElementDataType.object) {
            dbType = "TEXT";
        } else if (dataType == ElementDataType.rowpath) {
            dbType = "TEXT";
        } else if (dataType == ElementDataType.string) {
            dbType = "TEXT";
        } else {
            throw new IllegalStateException("unexpected ElementDataType: " + dataType.name());
        }
        //@formatter:off
        createTableCmdWithCols.append(", ").append(column.getElementKey()).append(" ").append(dbType)
                .append(" NULL");
        //@formatter:on
    }

    createTableCmdWithCols.append(");");

    db.execSQL(createTableCmdWithCols.toString());

    // Create the metadata for the table - table def and KVS
    createDBTableMetadata(db, tableId);

    // Now need to call the function to write out all the column values
    for (ColumnDefinition column : orderedDefs) {
        createNewColumnMetadata(db, tableId, column);
    }

    // Need to address column order
    ContentValues cvTableVal = new ContentValues();
    cvTableVal.put(KeyValueStoreColumns.TABLE_ID, tableId);
    cvTableVal.put(KeyValueStoreColumns.PARTITION, KeyValueStoreConstants.PARTITION_TABLE);
    cvTableVal.put(KeyValueStoreColumns.ASPECT, KeyValueStoreConstants.ASPECT_DEFAULT);
    cvTableVal.put(KeyValueStoreColumns.KEY, KeyValueStoreConstants.TABLE_COL_ORDER);
    cvTableVal.put(KeyValueStoreColumns.VALUE_TYPE, "array");

    StringBuilder tableDefCol = new StringBuilder();

    boolean needsComma = false;
    for (ColumnDefinition def : orderedDefs) {
        if (!def.isUnitOfRetention()) {
            continue;
        }
        if (needsComma) {
            tableDefCol.append(",");
        }
        needsComma = true;
        tableDefCol.append("\"").append(def.getElementKey()).append("\"");
    }

    WebLogger.getLogger(appName).i(t, "Column order for table " + tableId + " is " + tableDefCol.toString());
    String colOrderVal = "[" + tableDefCol.toString() + "]";
    cvTableVal.put(KeyValueStoreColumns.VALUE, colOrderVal);

    // Now add Tables values into KVS
    db.replaceOrThrow(DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME, null, cvTableVal);
}