public void setNumbers( List<ContactTokenDetails> activeTokens, Collection<String> inactiveTokens) { long timestamp = System.currentTimeMillis(); SQLiteDatabase db = databaseHelper.getWritableDatabase(); db.beginTransaction(); try { for (ContactTokenDetails token : activeTokens) { Log.w("Directory", "Adding active token: " + token.getNumber() + ", " + token.getToken()); ContentValues values = new ContentValues(); values.put(NUMBER, token.getNumber()); values.put(REGISTERED, 1); values.put(TIMESTAMP, timestamp); values.put(RELAY, token.getRelay()); values.put(VOICE, token.isVoice()); db.replace(TABLE_NAME, null, values); } for (String token : inactiveTokens) { ContentValues values = new ContentValues(); values.put(NUMBER, token); values.put(REGISTERED, 0); values.put(TIMESTAMP, timestamp); db.replace(TABLE_NAME, null, values); } db.setTransactionSuccessful(); } finally { db.endTransaction(); } }
@Override public void bulkUpdate( String tableName, List<ContentValues> contentValuesList, String key, List<Integer> deleteList) { long id; try { if (database == null) { database = helper.getWritableDatabase(); } for (ContentValues values : contentValuesList) { id = database.replace(tableName, null, values); if (id == -1) { CommonLog.e(CLASSTAG, "bulk update err, value: " + values.toString()); } } for (int values : deleteList) { database.execSQL("DELETE FROM " + tableName + " WHERE " + key + " = " + values); } database.setTransactionSuccessful(); } catch (Exception e) { e.printStackTrace(); } finally { if (database != null) { database.endTransaction(); } } }
public void setNumber(ContactTokenDetails token, boolean active) { SQLiteDatabase db = databaseHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(NUMBER, token.getNumber()); values.put(RELAY, token.getRelay()); values.put(REGISTERED, active ? 1 : 0); values.put(TIMESTAMP, System.currentTimeMillis()); db.replace(TABLE_NAME, null, values); }
@Override public Uri insert(Uri uri, ContentValues values) { final SQLiteDatabase db = dbHelper.getWritableDatabase(); final int match = uriMatcher.match(uri); Uri returnUri; switch (match) { case QUEUE: { long _id = db.replace(AlexandriaContract.QueueEntry.TABLE_NAME, null, values); if (_id > 0) { returnUri = AlexandriaContract.QueueEntry.CONTENT_URI; } else { throw new android.database.SQLException("Failed to insert row into " + uri); } break; } case BOOK: { long _id = db.insert(AlexandriaContract.BookEntry.TABLE_NAME, null, values); if (_id > 0) { db.delete( AlexandriaContract.QueueEntry.TABLE_NAME, AlexandriaContract.QueueEntry._ID + " = '" + Long.toString(_id) + "'", null); returnUri = AlexandriaContract.BookEntry.buildBookUri(_id); } else { throw new android.database.SQLException("Failed to insert row into " + uri); } getContext() .getContentResolver() .notifyChange(AlexandriaContract.BookEntry.buildFullBookUri(_id), null); getContext() .getContentResolver() .notifyChange(AlexandriaContract.BookEntry.CONTENT_URI, null); break; } case AUTHOR: { long _id = db.insert(AlexandriaContract.AuthorEntry.TABLE_NAME, null, values); if (_id > 0) returnUri = AlexandriaContract.AuthorEntry.buildAuthorUri(values.getAsLong("_id")); else throw new android.database.SQLException("Failed to insert row into " + uri); break; } case CATEGORY: { long _id = db.insert(AlexandriaContract.CategoryEntry.TABLE_NAME, null, values); if (_id > 0) returnUri = AlexandriaContract.CategoryEntry.buildCategoryUri(values.getAsLong("_id")); else throw new android.database.SQLException("Failed to insert row into " + uri); break; } default: throw new UnsupportedOperationException("Unknown uri: " + uri); } return returnUri; }
public synchronized boolean setValue(String name, String value) { Log.e(TAG, "setValue - name " + name + " value " + value); ContentValues values = new ContentValues(); values.put("name", name); values.put("value", value); SQLiteDatabase db = getWritableDatabase(); boolean success = db.replace(TABLE_MAP, null, values) != -1; if (readingThreads == 0 && db != null && db.isOpen()) db.close(); return success; }
/** * 保存一个联系人 * * @param user */ public void saveContact(User user) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_NAME_ID, user.getUsername()); if (user.getNick() != null) values.put(COLUMN_NAME_NICK, user.getNick()); if (user.getAvatar() != null) values.put(COLUMN_NAME_AVATAR, user.getAvatar()); if (db.isOpen()) { db.replace(TABLE_NAME, null, values); } }
protected void batchInsert(String tableName, List<ContentValues> cvs) { SQLiteDatabase db = mDbHelper.getWritableDatabase(); db.beginTransaction(); try { for (ContentValues cv : cvs) { db.replace(tableName, null, cv); } db.setTransactionSuccessful(); } finally { db.endTransaction(); } }
@Override public void store(@NonNull SQLiteDatabase db, @NonNull List<? extends Page> mPages) { if (mPages.isEmpty()) { return; } ContentValues pageValues = new ContentValues(14); ContentValues authorValues = new ContentValues(3); for (Page mPage : mPages) { List<Page> pages = new ArrayList<>(); pages.add(mPage); pages.addAll(mPage.getSubPages()); for (Page page : pages) { pageValues.clear(); pageValues.put(CacheHelper.PAGE_ID, page.getId()); pageValues.put(CacheHelper.PAGE_TITLE, page.getTitle()); pageValues.put(CacheHelper.PAGE_TYPE, page.getType()); pageValues.put(CacheHelper.PAGE_STATUS, page.getStatus()); pageValues.put(CacheHelper.PAGE_MODIFIED, page.getModified()); pageValues.put(CacheHelper.PAGE_DESCRIPTION, page.getDescription()); pageValues.put(CacheHelper.PAGE_CONTENT, page.getContent()); pageValues.put(CacheHelper.PAGE_PARENT_ID, page.getParentId()); pageValues.put(CacheHelper.PAGE_ORDER, page.getOrder()); pageValues.put(CacheHelper.PAGE_THUMBNAIL, page.getThumbnail()); pageValues.put(CacheHelper.PAGE_LOCATION, mLocation.getId()); pageValues.put(CacheHelper.PAGE_LANGUAGE, mLanguage.getId()); pageValues.put(CacheHelper.PAGE_AUTHOR, page.getAuthor().getLogin()); pageValues.put(CacheHelper.PAGE_AUTO_TRANSLATED, page.isAutoTranslated() ? 1 : 0); db.replace(CacheHelper.TABLE_PAGE, null, pageValues); authorValues.clear(); authorValues.put(CacheHelper.AUTHOR_USERNAME, page.getAuthor().getLogin()); authorValues.put(CacheHelper.AUTHOR_FIRSTNAME, page.getAuthor().getFirstName()); authorValues.put(CacheHelper.AUTHOR_LASTNAME, page.getAuthor().getLastName()); db.replace(CacheHelper.TABLE_AUTHOR, null, authorValues); mAvailableLanguageResource.store(db, page); } } }
public LocalGroup save(LocalAccount account, Group group) { if (isNull(account) || isNull(group)) { return null; } LocalGroup result = getGroup(account, group); if (result != null) { return result; } SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase(); sqLiteDatabase.beginTransaction(); try { long remoteGroupId = 0l; Date createdAt = new Date(); int state = LocalGroup.STATE_ADDED; if (group instanceof LocalGroup) { LocalGroup localGroup = (LocalGroup) group; remoteGroupId = localGroup.getRemoteGroupId(); createdAt = localGroup.getCreatedAt(); state = localGroup.getState(); result = localGroup; } else { result = new LocalGroup(); } ContentValues values = new ContentValues(); values.put("SP_Group_ID", group.getId()); values.put("Remote_Group_ID", remoteGroupId); values.put("Group_Name", group.getName()); values.put("Created_At", createdAt.getTime()); values.put("State", state); values.put("Account_ID", account.getAccountId()); long rowId = sqLiteDatabase.replace(TABLE, null, values); if (group instanceof LocalGroup) { result.setGroupId(rowId); result.setId(group.getId()); result.setRemoteGroupId(remoteGroupId); result.setName(group.getName()); result.setCreatedAt(createdAt); result.setState(state); result.setAccountId(account.getAccountId()); } sqLiteDatabase.setTransactionSuccessful(); } finally { sqLiteDatabase.endTransaction(); } return result; }
public long tagBookmark(long bookmarkId, long tagId) { if (mDb == null) { open(); if (mDb == null) { return -1; } } ContentValues values = new ContentValues(); values.put(BookmarkTagTable.BOOKMARK_ID, bookmarkId); values.put(BookmarkTagTable.TAG_ID, tagId); return mDb.replace(BookmarkTagTable.TABLE_NAME, null, values); }
/** * Updates the quantity of the given product to the given value. If the given value is zero, then * the product is removed from the table. * * @param productId the product to update * @param quantity the number of times the product has been purchased */ private void updatePurchasedItem(String productId, int quantity) { if (quantity == 0) { mDb.delete( PURCHASED_ITEMS_TABLE_NAME, PURCHASED_PRODUCT_ID_COL + "=?", new String[] {pidxor(productId)}); return; } ContentValues values = new ContentValues(); values.put(PURCHASED_PRODUCT_ID_COL, pidxor(productId)); // Encrpyted ProductID values.put(PURCHASED_QUANTITY_COL, quantity); mDb.replace(PURCHASED_ITEMS_TABLE_NAME, null /* nullColumnHack */, values); }
@Override public long[] store(List<Entry> items) { if (items == null) { return null; } long[] ids = new long[items.size()]; int current = 0; for (Entry entry : items) { List<Entry> similarEntries = getSimilarEntries(entry); // check if item not already added, if so, only continue if id is equal if (similarEntries != null && !similarEntries.isEmpty()) { boolean shouldUpdate = false; for (Entry sEntry : similarEntries) { if (sEntry.getId().equals(entry.getId())) { shouldUpdate = true; } break; } if (!shouldUpdate) { return null; } } ContentValues values = new ContentValues(); values.put(ENTRY_ID, entry.getId()); Long categoryId = entry.getCategoryId() != null ? entry.getCategoryId() : mCategoryId; Long feedId = entry.getFeedId() != null ? entry.getFeedId() : mFeedId; values.put(ENTRY_CATEGORY_ID, categoryId); values.put(ENTRY_FEED_ID, feedId); values.put(ENTRY_TITLE, entry.getTitle()); values.put(ENTRY_DESCRIPTION, entry.getDescription()); values.put(ENTRY_DATE, entry.getDate()); values.put(ENTRY_SRC_NAME, entry.getSrcName()); values.put(ENTRY_URL, entry.getLink()); values.put(ENTRY_SHORTENED_URL, entry.getShortenedLink()); values.put(ENTRY_IMAGE_URL, entry.getImageLink()); values.put(ENTRY_VISIBLE, entry.isVisible() ? 1 : 0); values.put(ENTRY_VISITED_DATE, entry.getVisitedDate()); values.put(ENTRY_FAVORITE_DATE, entry.getFavoriteDate()); values.put(ENTRY_IS_EXPANDED, entry.isExpanded() ? 1 : 0); /* * Inserting Row */ long rowId = db.replace(TABLE_ENTRY, null, values); ids[current++] = rowId; entry.setId(rowId); } return ids; }
/** * Inserts a purchased product into the database. There may be multiple rows in the table for the * same product if it was purchased multiple times or if it was refunded. * * @param orderId the order ID (matches the value in the product list) * @param productId the product ID (sku) * @param state the state of the purchase * @param purchaseTime the purchase time (in milliseconds since the epoch) * @param developerPayload the developer provided "payload" associated with the order. */ private void insertOrder( String orderId, String productId, PurchaseState state, long purchaseTime, String developerPayload) { ContentValues values = new ContentValues(); values.put(HISTORY_ORDER_ID_COL, orderId); values.put(HISTORY_PRODUCT_ID_COL, pidxor(productId)); values.put(HISTORY_STATE_COL, state.ordinal()); values.put(HISTORY_PURCHASE_TIME_COL, purchaseTime); values.put(HISTORY_DEVELOPER_PAYLOAD_COL, developerPayload); mDb.replace(PURCHASE_HISTORY_TABLE_NAME, null /* nullColumnHack */, values); }
@Override public boolean addItem(String tableName, ContentValues values) { boolean flag = false; long id; try { if (database == null) { database = helper.getWritableDatabase(); } id = database.replace(tableName, null, values); flag = (id != -1 ? true : false); } catch (Exception e) { e.printStackTrace(); } return flag; }
@Test public void testReplace() throws Exception { long id = addChuck(); assertThat(id).isNotEqualTo(-1L); ContentValues values = new ContentValues(); values.put("id", id); values.put("name", "Norris"); long replaceId = database.replace("table_name", null, values); assertThat(replaceId).isEqualTo(id); String query = "SELECT name FROM table_name where id = " + id; Cursor cursor = executeQuery(query); assertThat(cursor.moveToNext()).isTrue(); assertThat(cursor.getString(cursor.getColumnIndex("name"))).isEqualTo("Norris"); }
public static void setPolicy(Context context, UidPolicy policy) { SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase(); getPackageInfoForUid(context, policy); ContentValues values = new ContentValues(); values.put("logging", policy.logging); values.put("uid", policy.uid); values.put("command", policy.command); values.put("policy", policy.policy); values.put("until", policy.until); values.put("name", policy.name); values.put("package_name", policy.packageName); values.put("desired_uid", policy.desiredUid); values.put("desired_name", policy.desiredName); values.put("username", policy.username); db.replace("uid_policy", null, values); db.close(); }
public void tagBookmark(long bookmarkId, List<Tag> tags) { if (mDb == null) { open(); if (mDb == null) { return; } } mDb.beginTransaction(); try { for (Tag t : tags) { if (t.mId < 0) { continue; } if (t.isChecked()) { ContentValues values = new ContentValues(); values.put(BookmarkTagTable.BOOKMARK_ID, bookmarkId); values.put(BookmarkTagTable.TAG_ID, t.mId); mDb.replace(BookmarkTagTable.TABLE_NAME, null, values); } else { mDb.delete( BookmarkTagTable.TABLE_NAME, BookmarkTagTable.BOOKMARK_ID + "=" + bookmarkId + " AND " + BookmarkTagTable.TAG_ID + "=" + t.mId, null); } } } catch (Exception e) { Log.d(TAG, "exception in tagBookmark", e); } mDb.setTransactionSuccessful(); mDb.endTransaction(); }
@Override public void bulkInsert(String tableName, List<ContentValues> contentValuesList) { long id; try { if (database == null) { database = helper.getWritableDatabase(); } database.beginTransaction(); for (ContentValues values : contentValuesList) { id = database.replace(tableName, null, values); if (id == -1) { CommonLog.e(CLASSTAG, "bulk insert err, value: " + values.toString()); } } database.setTransactionSuccessful(); } catch (Exception e) { e.printStackTrace(); } finally { if (database != null) { database.endTransaction(); } } }
public void update(String url, String content) { db.replace("cache", null, buildContentValues(url, content)); }
public void setProperty(SQLiteDatabase db, String key, String value) { final ContentValues values = new ContentValues(); values.put(PropertiesColumns.PROPERTY_KEY, key); values.put(PropertiesColumns.PROPERTY_VALUE, value); db.replace(Tables.PROPERTIES, null, values); }
private void setProperty(SQLiteDatabase db, String key, String value) { ContentValues values = new ContentValues(); values.put(PropertiesColumns.PROPERTY_KEY, key); values.put(PropertiesColumns.PROPERTY_VALUE, value); db.replace(SizzoSchema.PROPERTIES.TABLE, null, values); }