下面列出了android.database.sqlite.SQLiteDatabase#update ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void moveAllToTab(int tabNumber, ArrayList<Pair<Integer, String>> ideas) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
switch (tabNumber) {
case 1: //NOW
values.put(DataEntry.COLUMN_NAME_DONE, false);
values.put(DataEntry.COLUMN_NAME_LATER, false);
break;
case 2: //LATER
values.put(DataEntry.COLUMN_NAME_DONE, false);
values.put(DataEntry.COLUMN_NAME_LATER, true);
break;
case 3: //DONE
values.put(DataEntry.COLUMN_NAME_LATER, false);
values.put(DataEntry.COLUMN_NAME_DONE, true);
break;
}
for (Pair<Integer, String> idea : ideas) {
db.update(DataEntry.TABLE_NAME, values, "_id=" + idea.first, null);
}
notifyAllLists();
}
public int updateRow(String TABLE_NAME,String[] columnKeys, String[] columnValues,String id) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
for(int i=0;i<columnKeys.length;i++){
values.put(columnKeys[i], columnValues[i]);
}
// updating row
return db.update( TABLE_NAME,
values,
KEY_ID + " = ?",
new String[] { id }
);
}
public void updateLoan(Loan loan){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
if(loan.isNotifying()){
values.put(LOAN_NOTIFY, 1);
} else {
values.put(LOAN_NOTIFY, 0);
}
values.put(LOAN_STARTDATE, loan.getStartDate().toString());
if(loan.getReturnDate()==null){
values.putNull(LOAN_RETURNDATE);
} else {
values.put(LOAN_RETURNDATE, loan.getReturnDate().toString());
}
values.put(LOAN_ITEMID, loan.getItemID());
values.put(LOAN_PERSONID, loan.getPersonID());
db.update(LOAN_TABLENAME, values, LOAN_ID+"=?", new String[]{Integer.toString(loan.getLoanID())});
db.close();
}
@Override
//更新数据
public int update(Uri uri, ContentValues values, String where, String[] whereArgs)
{
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
int count;
switch (sUriMatcher.match(uri))
{
case CALENDAR:
count = db.update(CalendarColumn_TABLE_NAME, values, where, whereArgs);
break;
case CALENDAR_ID:
String noteId = uri.getPathSegments().get(1);
count = db.update(CalendarColumn_TABLE_NAME, values, CalendarColumn._ID + "=" + noteId + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
/**
* Updates the given channel's last visit time.
*
* @param channelId Channel ID
*
* @return last visit time, if the update was successful; -1 otherwise.
*/
public long updateLastVisitTime(String channelId) {
SQLiteDatabase db = getWritableDatabase();
long currentTime = System.currentTimeMillis();
ContentValues values = new ContentValues();
values.put(SubscriptionsTable.COL_LAST_VISIT_TIME, currentTime);
int count = db.update(
SubscriptionsTable.TABLE_NAME,
values,
SubscriptionsTable.COL_CHANNEL_ID + " = ?",
new String[]{channelId});
return (count > 0 ? currentTime : -1);
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count = 0;
SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
//always update local database
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
count = db.update(args.table, values, args.where, args.args);
if (count > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return count;
}
public static void endRow(DownloadTask downloadTask) {
SQLiteDatabase db = null;
try {
DbHelper dbHelper = new DbHelper(App.getInstance());
db = dbHelper.getWritableDatabase();
// db.beginTransaction();
ContentValues cv = new ContentValues();
cv.put(COLUMN_ENDDATETIME, DbHelper.DateTimeFormat.format(downloadTask.getStateChangedDate()));
cv.put(COLUMN_STATE, downloadTask.getState());
cv.put(COLUMN_DOWNLOADEDCONTENTLENGTH, downloadTask.getDownloadedSize());
db.update(TABLE_NAME, cv, COLUMN_ID + "=" + downloadTask.getId(), null);
} catch (Exception ex) {
AppLog.e(App.getInstance(), ex);
} finally {
if (db != null) {
// db.endTransaction();
db.close();
}
}
}
@Override
public void setDownloadingStatesToQueued() throws DatabaseIOException {
ensureInitialized();
try {
ContentValues values = new ContentValues();
values.put(COLUMN_STATE, Download.STATE_QUEUED);
SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
writableDatabase.update(tableName, values, WHERE_STATE_IS_DOWNLOADING, /* whereArgs= */ null);
} catch (SQLException e) {
throw new DatabaseIOException(e);
}
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = mOPenHelper.getWritableDatabase();
int count;
String finalWhere;
switch (sUriMatcher.match(uri)){
case SETTINGS:
count = db.update(
Settings.TABLE_NAME,
values,
selection,
selectionArgs
);
break;
case SETTINGS_ID:
String id = uri.getPathSegments().get(1);
finalWhere = Settings._ID +" = "+id;
if(selection != null){
finalWhere += " AND " +selection;
}
count = db.update(Settings.TABLE_NAME,
values,
finalWhere,
selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown uri: "+uri);
}
getContext().getContentResolver().notifyChange(uri,null);
return count;
}
public void updateCapabilitiesETag(long accountId, String capabilitiesETag) {
validateAccountId(accountId);
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues(1);
values.put(key_capabilities_etag, capabilitiesETag);
final int updatedRows = db.update(table_accounts, values, key_id + " = ?", new String[]{String.valueOf(accountId)});
if (updatedRows == 1) {
Log.v(TAG, "Updated etag to " + capabilitiesETag + " for accountId = " + accountId);
} else {
Log.e(TAG, "Updated " + updatedRows + " but expected only 1 for accountId = " + accountId + " and capabilitiesETag = " + capabilitiesETag);
}
}
/**
* Saves a draft.
*
* @param draft
* The draft to save.
*/
public void saveDraft(Draft draft) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Draft.COLUMN_SEND_WHEN_ONLINE, draft.getSendWhenOnline());
values.put(Draft.COLUMN_TYPE, draft.getType());
values.put(Draft.COLUMN_ACCOUNT, draft.getAccount());
values.put(Draft.COLUMN_NAME, draft.getName());
values.put(Draft.COLUMN_BODY, draft.getBody());
values.put(Draft.COLUMN_IMAGE, draft.getImage());
values.put(Draft.COLUMN_CAPTION, draft.getCaption());
values.put(Draft.COLUMN_VIDEO, draft.getVideo());
values.put(Draft.COLUMN_AUDIO, draft.getAudio());
values.put(Draft.COLUMN_TAGS, draft.getTags());
values.put(Draft.COLUMN_URL, draft.getUrl());
values.put(Draft.COLUMN_START_DATE, draft.getStartDate());
values.put(Draft.COLUMN_END_DATE, draft.getEndDate());
values.put(Draft.COLUMN_SYNDICATION_TARGETS, draft.getSyndicationTargets());
values.put(Draft.COLUMN_PUBLISH_DATE, draft.getPublishDate());
values.put(Draft.COLUMN_LOCATION_NAME, draft.getLocationName());
values.put(Draft.COLUMN_LOCATION_URL, draft.getLocationUrl());
values.put(Draft.COLUMN_LOCATION_VISIBILITY, draft.getLocationVisibility());
values.put(Draft.COLUMN_SPINNER, draft.getSpinner());
values.put(Draft.COLUMN_COORDINATES, draft.getCoordinates());
if (draft.getId() > 0) {
db.update(Draft.TABLE_NAME, values, Draft.COLUMN_ID + "=" + draft.getId(), null);
}
else {
db.insert(Draft.TABLE_NAME, null, values);
}
db.close();
}
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
//values.put(KEY_NAME, contact.getName());
values.put(KEY_PH_NO, contact.getPhoneNumber());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
protected void updateChat(String uuid, Chat newData){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = chatToContentValues(newData);
String selection = ChatTable.UUID + " = ?";
db.update(ChatTable.TABLE_NAME, values, selection, new String[]{ uuid });
}
private Team updateExistingTeam(SQLiteDatabase db, Team team) {
Logger.d("Updating team with the name of '" + team.getName() + "'");
ContentValues values = new ContentValues();
values.put(TEAMS_NAME, team.getName());
long id = db.update(TEAMS_TABLE_NAME, values, _ID + " = ?", new String[]{team.getId().toString()});
return new Team(id, team.getName());
}
public void markAsNotified(long id) {
SQLiteDatabase database = databaseHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NOTIFIED, 1);
database.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {String.valueOf(id)});
}
public int update(String table, Map<String, Object> attrs, String whereClause, String[] whereArgs) {
try {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = transferMap(attrs);
return db.update(table, values, whereClause, whereArgs);
} catch (Exception ex) {
LOGGER.w("failed to execute update instrument. cause: " + ex.getMessage());
return 0;
}
}
public void updateContactPassword(Integer id, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("password", password);
db.update("user", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
}
/**
* Update topic description
*
* @return true if the record was updated, false otherwise
*/
public static boolean update(SQLiteDatabase db, Topic topic) {
StoredTopic st = (StoredTopic) topic.getLocal();
if (st == null) {
return false;
}
BaseDb.Status status = st.status;
// Convert topic description to a map of values
ContentValues values = new ContentValues();
if (st.status == BaseDb.Status.QUEUED && !topic.isNew()) {
status = BaseDb.Status.SYNCED;
values.put(COLUMN_NAME_STATUS, status.value);
values.put(COLUMN_NAME_TOPIC, topic.getName());
}
if (topic.getUpdated() != null) {
values.put(COLUMN_NAME_UPDATED, topic.getUpdated().getTime());
}
values.put(COLUMN_NAME_READ, topic.getRead());
values.put(COLUMN_NAME_RECV, topic.getRecv());
values.put(COLUMN_NAME_SEQ, topic.getSeq());
values.put(COLUMN_NAME_CLEAR, topic.getClear());
values.put(COLUMN_NAME_ACCESSMODE, BaseDb.serializeMode(topic.getAccessMode()));
values.put(COLUMN_NAME_DEFACS, BaseDb.serializeDefacs(topic.getDefacs()));
values.put(COLUMN_NAME_TAGS, BaseDb.serializeStringArray(topic.getTags()));
if (topic.getLastSeen() != null) {
values.put(COLUMN_NAME_LAST_SEEN, topic.getLastSeen().getTime());
}
if (topic.getLastSeenUA() != null) {
values.put(COLUMN_NAME_LAST_SEEN_UA, topic.getLastSeenUA());
}
if (topic instanceof MeTopic) {
values.put(COLUMN_NAME_CREDS, BaseDb.serialize(((MeTopic) topic).getCreds()));
}
values.put(COLUMN_NAME_PUBLIC, BaseDb.serialize(topic.getPub()));
values.put(COLUMN_NAME_PRIVATE, BaseDb.serialize(topic.getPriv()));
Date lastUsed = topic.getTouched();
if (lastUsed != null) {
values.put(COLUMN_NAME_LASTUSED, lastUsed.getTime());
}
int updated = db.update(TABLE_NAME, values, _ID + "=" + st.id, null);
if (updated > 0) {
if (lastUsed != null) {
st.lastUsed = lastUsed;
}
st.status = status;
}
return updated > 0;
}
@Override
public int update(final Uri uri, final ContentValues values,
final String where, final String[] whereArgs) {
// log("update() uri = " + uri + " where= (" + where + ") whereArgs = "
// + StringHelper.toString(whereArgs));
final SQLiteDatabase db = this.dbHelper.getWritableDatabase();
int count;
String id;
String _id;
switch (FanFouProvider.sUriMatcher.match(uri)) {
case USER_ID:
id = uri.getPathSegments().get(2);
count = db.update(UserInfo.TABLE_NAME, values, BasicColumns.ID
+ "=?", new String[] { id });
break;
case STATUS_ID:
id = uri.getPathSegments().get(2);
count = db.update(StatusInfo.TABLE_NAME, values, BasicColumns.ID
+ "=?", new String[] { id });
// count = db.update(StatusInfo.TABLE_NAME, values,
// StatusInfo.ID
// + "="
// + statusId
// + (!TextUtils.isEmpty(where) ? " AND (" + where
// + ')' : ""), whereArgs);
break;
case MESSAGE_ITEM:
id = uri.getPathSegments().get(2);
count = db.update(DirectMessageInfo.TABLE_NAME, values,
BasicColumns.ID + "=?", new String[] { id });
// count = db.update(
// DirectMessageInfo.TABLE_NAME,
// values,
// DirectMessageInfo.ID
// + "="
// + messageId
// + (!TextUtils.isEmpty(where) ? " AND (" + where
// + ')' : ""), whereArgs);
break;
case MESSAGE_ID:
_id = uri.getPathSegments().get(2);
id = _id;
count = db.update(DirectMessageInfo.TABLE_NAME, values,
BaseColumns._ID + "=?", new String[] { _id });
break;
case USERS_ALL:
id = "";
count = db.update(UserInfo.TABLE_NAME, values, where, whereArgs);
break;
case USER_TYPE:
id = "";
count = db.update(UserInfo.TABLE_NAME, values, BasicColumns.TYPE
+ "=?", new String[] { uri.getPathSegments().get(2) });
break;
case STATUSES_ALL:
id = "";
count = db.update(StatusInfo.TABLE_NAME, values, where, whereArgs);
break;
// case PUBLIC:
// id = "";
// count = db.update(StatusInfo.PUBLIC_TABLE_NAME, values, where,
// whereArgs);
// break;
case MESSAGES_ALL:
id = "";
count = db.update(DirectMessageInfo.TABLE_NAME, values, where,
whereArgs);
break;
case DRAFT_ALL:
case DRAFT_ID:
throw new UnsupportedOperationException(
"unsupported update action: " + uri);
default:
throw new IllegalArgumentException("update() Unknown URI " + uri);
}
if (AppContext.DEBUG) {
if (count > 0) {
log("update() result uri=" + uri + " id=" + id + " count="
+ count);
}
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
public void addHistoryItemDetails(String itemID, String itemDetails) {
// As we're going to do an update only we don't need need to worry
// about the preferences; if the item wasn't saved it won't be udpated
SQLiteOpenHelper helper = new DBHelper(activity);
SQLiteDatabase db = null;
Cursor cursor = null;
try {
db = helper.getWritableDatabase();
cursor = db.query(DBHelper.TABLE_NAME,
ID_DETAIL_COL_PROJECTION,
DBHelper.TEXT_COL + "=?",
new String[] { itemID },
null,
null,
DBHelper.TIMESTAMP_COL + " DESC",
"1");
String oldID = null;
String oldDetails = null;
if (cursor.moveToNext()) {
oldID = cursor.getString(0);
oldDetails = cursor.getString(1);
}
if (oldID != null) {
String newDetails;
if (oldDetails == null) {
newDetails = itemDetails;
} else if (oldDetails.contains(itemDetails)) {
newDetails = null;
} else {
newDetails = oldDetails + " : " + itemDetails;
}
if (newDetails != null) {
ContentValues values = new ContentValues();
values.put(DBHelper.DETAILS_COL, newDetails);
db.update(DBHelper.TABLE_NAME, values, DBHelper.ID_COL + "=?", new String[] { oldID });
}
}
} finally {
close(cursor, db);
}
}