Code example for SQLiteDatabase

Methods: deleteinsertrawQueryupdate

0
	} 
 
	public void Update(int id, int newType) {
		ContentValues cv = new ContentValues();
		cv.put(PLAKATE_TYPE, newType);
		mDatabase.update(TABLE_PLAKATE, cv, PLAKATE_ID + "=?",
				new String[] { String.valueOf(id) });
		try { 
			if (getChangeType(id, CHANGE_TYPE_CHANGED) != CHANGE_TYPE_NEW) {
				mDatabase.delete(TABLE_CHANGES, CHANGES_ID + "=?", new String[] { String.valueOf(id) });
				ContentValues values = new ContentValues();
				values.put(CHANGES_ID, id);
				values.put(CHANGES_TYPE, CHANGE_TYPE_CHANGED);
				mDatabase.insert(TABLE_CHANGES, null, values);
			} 
		} catch (SQLException ex) {
			// May fail cause we already have a row with "INSERT" as changetype 
		} 
	} 
 
	public void GetChangedItems(List<PlakatOverlayItem> inserted,
			List<PlakatOverlayItem> changed, List<Integer> deleted) {
		Cursor changes = mDatabase.query(TABLE_CHANGES, null, null, null, null,
				null, null); 
		if (changes != null) {
			try { 
				changes.moveToFirst();
				int idx_id = changes.getColumnIndex(CHANGES_ID);
				int idx_typ = changes.getColumnIndex(CHANGES_TYPE);
 
				while (!changes.isAfterLast()) {
					int id = changes.getInt(idx_id);
					int typ = changes.getInt(idx_typ);
					if (typ == CHANGE_TYPE_NEW)
						inserted.add(getOverlayItem(id));
					else if (typ == CHANGE_TYPE_CHANGED)
						changed.add(getOverlayItem(id));
					else if (typ == CHANGE_TYPE_DELETED)
						deleted.add(new Integer(id));
 
					changes.moveToNext();
				} 
			} finally { 
				changes.close();
			} 
		} 
	} 
 
	public void delete(int id) {
		String[] sid = new String[] { String.valueOf(id) };
		mDatabase.delete(TABLE_PLAKATE, PLAKATE_ID + "=?", sid);
		if (getChangeType(id, CHANGE_TYPE_CHANGED) != CHANGE_TYPE_DELETED) {
			mDatabase.delete(TABLE_CHANGES, CHANGES_ID + "=?", sid);
			ContentValues values = new ContentValues();
			values.put(CHANGES_ID, id);
			values.put(CHANGES_TYPE, CHANGE_TYPE_DELETED);
			mDatabase.insert(TABLE_CHANGES, null, values);
		} 
	} 
 
	public void ClearData(int id) {
		String[] sid = new String[] { String.valueOf(id) };
		mDatabase.delete(TABLE_PLAKATE, PLAKATE_ID + "=?", sid);
		mDatabase.delete(TABLE_CHANGES, CHANGES_ID + "=?", sid);
	} 
	 
	public void ClearAllData() { 
		mDatabase.delete(TABLE_PLAKATE, null, null);
		mDatabase.delete(TABLE_CHANGES, null, null);
	} 
 
	public PlakatOverlayItem getOverlayItem(int id) {
		Cursor crs = mDatabase.query(TABLE_PLAKATE, null, PLAKATE_ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null);
		try { 
			if (crs != null && crs.moveToFirst()) {
				return loadFromCursor(crs);
			} 
		} finally { 
			crs.close();
		} 
		return null; 
	} 
 
	private int getNextId() { 
		Cursor dataCount = mDatabase.rawQuery("select max(" + PLAKATE_ID
				+ ") from " + TABLE_PLAKATE, null);
		dataCount.moveToFirst();
		try { 
			return dataCount.getInt(0) + 1;
		} finally { 
			dataCount.close();
		} 
	} 
 
	private int getChangeType(int id, int defaultValue) {
		Cursor dataCount = mDatabase.rawQuery("select " + CHANGES_TYPE
				+ " from " + TABLE_CHANGES + " WHERE " + CHANGES_ID + " = "
				+ id, null);
 
		dataCount.moveToFirst();
		try {