Code example for SQLiteDatabase

Methods: isOpenisReadOnlyupdate, close

0
	/** 
	 * Open database by writable mode, if needed. 
	 */ 
	protected void openWritable() { 
 
		if (mDb != null && mDb.isOpen()) {
 
			if (!mDb.isReadOnly())
				return; 
 
			mDb.close();
 
		} 
 
		mDb = getWritableDatabase();
 
	} 
 
	/** 
	 * Get database controller. Override is required on sub-class. Sub-class 
	 * returns extending database controller. 
	 * 
	 * @return Extending database controller. 
	 */ 
	public abstract IDatabaseController getDatabaseController(); 
 
	/** 
	 * The interface of database controller. 
	 * 
	 * @author h.saitou 
	 */ 
	public interface IDatabaseController { 
 
		/** 
		 * Set query parameters of selection. This parameters is backuped by 
		 * database controller. 
		 * 
		 * @param selection 
		 *            Selecton paremter of query. 
		 * @param selectionArgs 
		 *            SelectionArgs parameter of query. 
		 */ 
		public void setQuerySelection(String selection, String[] selectionArgs);
 
		/** 
		 * Set query parameters of order by. this parameters is backuped by 
		 * database controller. 
		 * 
		 * @param orderBy 
		 *            OrderBy parametr of query. 
		 */ 
		public void setQueryOrderBy(String orderBy);
 
		/** 
		 * Execute sql query command. 
		 * 
		 * @param columns 
		 *            Target columns. 
		 * @return The cursor of query. 
		 */ 
		public Cursor query(String[] columns);
 
		/** 
		 * Execute sql insert command. 
		 * 
		 * @param values 
		 *            The parameters of insert command. 
		 * @param nullColumnHack 
		 *            The parameters of insert command. 
		 * @return The number of affected records. 
		 */ 
		public long insert(ContentValues values, String nullColumnHack);
 
		/** 
		 * Execute sql update command. 
		 * 
		 * @param values 
		 *            The parameters of update command. 
		 * @param whereClause 
		 *            The parameters of update command. 
		 * @param whereArgs 
		 *            The parameters of update command. 
		 * @return The number of affected records. 
		 */ 
		public int update(ContentValues values, String whereClause, String[] whereArgs);
 
		/** 
		 * Execute sql delete command. 
		 * 
		 * @param whereClause 
		 *            The parameters of delete command. 
		 * @param whereArgs 
		 *            The parameters of delete command. 
		 * @return The number of affected records. 
		 */ 
		public int delete(String whereClause, String[] whereArgs);
 
	} 
 
	/** 
	 * The class is super-class of database controller. 
	 * 
	 * @author h.saitou 
	 */ 
	protected abstract class AbsDatabaseController implements IDatabaseController { 
 
		private String mTableName;
		private String mSelection = null;
		private String[] mSelectionArgs = null;
		private String mOrderBy = null;
 
		/** 
		 * Constructor. 
		 * 
		 * @param tableName 
		 *            The name of table that is managed by database controller. 
		 */ 
		public AbsDatabaseController(String tableName) {
			mTableName = tableName;
		} 
 
		public void setQuerySelection(String selection, String[] selectionArgs) {
 
			mSelection = selection;
			mSelectionArgs = selectionArgs;
 
		} 
 
		public void setQueryOrderBy(String orderBy) {
 
			mOrderBy = orderBy;
 
		} 
 
		public Cursor query(String[] columns) {
 
			openReadable(); 
			return mDb.query(mTableName, columns, mSelection, mSelectionArgs, null, null, mOrderBy);
 
		} 
 
		public long insert(ContentValues values, String nullColumnHack) {
 
			openWritable(); 
			return mDb.insert(mTableName, null, values);
 
		} 
 
		public int update(ContentValues values, String whereClause, String[] whereArgs) {
 
			openWritable(); 
			return mDb.update(mTableName, values, whereClause, whereArgs);
 
		} 
 
		public int delete(String whereClause, String[] whereArgs) {