カテゴリー:
日記アプリ
閲覧数:453 配信日:2014-01-14 17:14
・SQLiteDatabaseオブジェクトを取得(読込み用にデータベースをオープン)
SQLiteDatabase db = helper.getReadableDatabase(); //SQLiteOpenHelperクラスのメソッド。SQLiteDatabaseオブジェクトを取得(読込み用にデータベースをオープン) |
・検索する条件を設定
// 検索する条件を設定 // RecordItemクラスのゲッターメソッド利用 // String searchNikki = RecordItem.getItemSearchNikki(); //服(検索キーワード) String searchNen = RecordItem.getItemSearchNen(); //2014 String searchTuki = RecordItem.getItemSearchTuki(); //1 String searchHi = RecordItem.getItemSearchHi(); // where句の条件 if ( searchWord == true ){ //itemnikki like '%服を% ※初回はfalse。 if ( searchNikki.length() > 0 ){ //実体はRecordItemクラスのStringプロパティitemsearchnikki。デフォルトnull searchCulmn = (RecordItem.COLUMN_ITEMNIKKI + " like '%" + searchNikki + "%' " ).toString(); } } else { //nen = '2014' AND tuki = '1' ; if ( searchHi == null ){ //実体はRecordItemクラスのStringプロパティitemsearchhi。デフォルトnull searchCulmn = RecordItem.COLUMN_ITEMNEN + " = '" + searchNen + "' AND " + RecordItem.COLUMN_ITEMTUKI + " = '" + searchTuki + "' " ; } else { searchCulmn = RecordItem.COLUMN_ITEMNEN + " = '" + searchNen + "' AND " + RecordItem.COLUMN_ITEMTUKI + " = '" + searchTuki + "' AND " + RecordItem.COLUMN_ITEMHI + " = '" + searchHi + "' " ; } } |
・where句の条件を得たら検索
・rawQueryメソッド
// where句の条件を得たら検索 try { String query = null ; query = "select * " + " from " + RecordItem.TABLE_NAME_ITEM + " where " + searchCulmn + ";" ; // "select * from tbl_androidstylesystem where nen = '2014' AND tuki = '1' ;" // "select * from tbl_androidstylesystem where itemnikki like '%服を%' ;" Cursor cursor = db.rawQuery(query, null ); |
・クラス型オブジェクト(ユーザー定義クラスRecordItemのオブジェクト)を格納するリストitemListを生成
itemList = new ArrayList<RecordItem>(); //クラス型オブジェクト(ユーザー定義クラスRecordItemのオブジェクト)を格納するリストitemListを生成 |
※上記参照
・cursorオブジェクトデバッグ
cursor SQLiteCursor (id= 830060762464 ) mClosed false mColumnNameMap null mColumns String[ 5 ] (id= 830060762824 ) [ 0 ] "_id" (id= 830060762864 ) [ 1 ] "itemnikki" (id= 830060762928 ) [ 2 ] "nen" (id= 830060763000 ) [ 3 ] "tuki" (id= 830060763064 ) [ 4 ] "hi" (id= 830060763128 ) mContentObservable ContentObservable (id= 830060762632 ) mContentResolver null mCount - 1 mCurrentRowID null mCursorState 0 mDatabase SQLiteDatabase (id= 830060758136 ) mDataSetObservable DataSetObservable (id= 830060762584 ) mDriver SQLiteDirectCursorDriver (id= 830060761768 ) mEditTable null mInitialRead 2147483647 mLock null mMaxRead 2147483647 mNotificationHandler null mNotifyUri null mPendingData false mPos - 1 mQuery SQLiteQuery (id= 830060762344 ) mRowIdColumnIndex 0 mSelfObserver null mSelfObserverLock Object (id= 830060762680 ) mSelfObserverRegistered false mStackTraceElements null mUpdatedRows HashMap (id= 830060762696 ) mWindow null itemList ArrayList (id= 830060763184 ) |
・検索結果の先頭レコードにカーソルを移動し、リストへデータ追加
cursor.moveToFirst(); //検索結果の先頭レコードにカーソルを移動 while ( !cursor.isAfterLast()){ itemList.add( getItem( cursor ) ); cursor.moveToNext(); } } finally { db.close(); } //itemList ArrayList (id=830060748640) //[0] RecordItem (id=830060751240) … DBより取得した該当レコード // itemhi "18" (id=830060752528) // itemnen "2014" (id=830060751336) // itemnikki "服を買う" (id=830060751272) // itemtuki "1" (id=830060751400) // rowid 3
return itemList; } |