Code example for SQLiteDatabase

Methods: insertOrThrowquery

0
	public long newTag(String tag) throws Exception {
		Log.i(TAG,"newTag("+tag+")");
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_TAG, tag);
		initialValues.put(KEY_USED_CACHE, 0);
		return mDb.insertOrThrow(TAGS_TABLE, null, initialValues);
	} 
 
	public long newTagPing(long ping_id, long tag_id) throws Exception {
		ContentValues init = new ContentValues();
		init.put(KEY_PID, ping_id);
		init.put(KEY_TID, tag_id);
		return mDb.insertOrThrow(TAG_PING_TABLE, null, init);
	} 
 
	public String getTagName(long tid) {
		String ret = "";
		Cursor c = mDb.query(TAGS_TABLE, new String[] {KEY_TAG}, 
				KEY_ROWID+"="+tid, null, null, null, null);
		if (c.getCount()>0) {
			c.moveToFirst();
			ret = c.getString(c.getColumnIndex(KEY_TAG));
		} 
		c.close();
		return ret;
	} 
	// round d to 5 sig digits 
	public static double round5(double d) {
		return (java.lang.Math.round(d*100000))/100000.0;
	} 
 
	private long getOrMakeNewTID(String tag) {
		long tid;
		try { 
			tid = newTag(tag);
		} catch (Exception e) {
			tid = getTID(tag);
		} 
		return tid;
	} 
 
	/** 
	 * Delete the note with the given rowId 
	 *  
	 * @param rowId id of note to delete 
	 * @return true if deleted, false otherwise 
	 */ 
	public boolean deletePing(long rowId) {
		return mDb.delete(PINGS_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	} 
 
	/** 
	 *  
	 * @param tag 
	 * @return tag_id of tag or -1 if tag does not exist 
	 */ 
	public long getTID(String tag) {
		Log.i(TAG,"getTID("+tag+")");
		// return -1 if not found 
		long tid = -1;
		Cursor cursor = 
			mDb.query(TAGS_TABLE, new String[] {KEY_ROWID, KEY_TAG}, 
					KEY_TAG + "='" + tag + "'", null,
					null, null, null, null); 
		Log.i(TAG,"queried for tag="+tag);
		if (cursor.getCount()>0) {