APIを使ってデータベースを操作します.


SQLiteDatabase類を使う
insert()
delete()
udate()
query()
コンテントValuesレコードの値
public class PersonDao2 {

	private static final String TAG = "PersonDao2";
	private PersonSQLiteOpenHelper mOpenHelper;	//          

	public PersonDao2(Context context) {
		mOpenHelper = new PersonSQLiteOpenHelper(context);
	}
	
	/**
	 *    person     
	 * @param person
	 */
	public void insert(Person person) {
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		if(db.isOpen()) {	//        ,        
			
			ContentValues values = new ContentValues();
			values.put("name", person.getName());		// key        , value     
			values.put("age", person.getAge());
			long id = db.insert("person", "name", values);
			Log.i(TAG, "id: " + id);
			
			db.close();	//      
		}
	}
	
	/**
	 *   id    
	 * @param id
	 */
	public void delete(int id) {
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();	//           
		if(db.isOpen()) {	//        ,        
			
			String whereClause = "_id = ?";
			String[] whereArgs = {id + ""};
			int count = db.delete("person", whereClause, whereArgs);
			Log.i(TAG, "   : " + count + " ");
			db.close();	//      
		}
	}
	

	/**
	 *   id    ,       
	 * @param id
	 * @param name
	 */
	public void update(int id, String name) {
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		if(db.isOpen()) {	//        ,        
			ContentValues values = new ContentValues();
			values.put("name", name);
			
			int count  = db.update("person", values, "_id = ?", new String[]{id + ""});

			Log.i(TAG, "   : " + count + " ");
			
			db.close();	//      
		}
	}
	
	public List queryAll() {
		SQLiteDatabase db = mOpenHelper.getReadableDatabase();	//             
		if(db.isOpen()) {
			String[] columns = {"_id", "name", "age"};	//     
			String selection = null;	//     ,  null    
			String[] selectionArgs = null;	//        ,         ?         
			String groupBy = null;	//       group by name
			String having = null;	//     
			String orderBy = null;	//   
			
			Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy);
			
			int id;
			String name;
			int age;
			if(cursor != null && cursor.getCount() > 0) {
				List personList = new ArrayList();
				
				while(cursor.moveToNext()) {	//      ,       ,         ,   .
					id = cursor.getInt(0);
					name = cursor.getString(1);
					age = cursor.getInt(2);
					
					personList.add(new Person(id, name, age));
				}
				
				db.close();
				return personList;
			}
			db.close();
		}
		return null;
	}
	
	/**
	 *   id   
	 * @param id
	 * @return
	 */
	public Person queryItem(int id) {
		SQLiteDatabase db = mOpenHelper.getReadableDatabase();	//             
		if(db.isOpen()) {
			String[] columns = {"_id", "name", "age"};	//     
			String selection = "_id = ?";	//     ,  null    
			String[] selectionArgs = {id + ""};	//        ,         ?         
			String groupBy = null;	//       group by name
			String having = null;	//     
			String orderBy = null;	//   
			
			Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy);
			
			if(cursor != null && cursor.moveToFirst()) {		// cursor    null,           
				int _id = cursor.getInt(0);
				String name = cursor.getString(1);
				int age = cursor.getInt(2);
				
				db.close();
				return new Person(_id, name, age);
			}
			db.close();
		}
		return null;
	}
}