SQLite で抽出データを並び替え
SELECT * FROM テーブル名 ORDER BY フィールド1 DESC, フィールド2 DESC, フィールド3 ASC ;
上記のようにすることで、「テーブル名」という名前のテーブルの全てのデータを「フィールド1」の降順で並べ、「フィールド1」が同値の場合、次の「フィールド2」を降順に並べ、さらに「フィールド2」も同値の場合、「フィールド3」を基準に昇順に並び替えます。
ASC は結果が昇順に並びかえ、 DESC はそれが降順に並ぶかえることを表します。そのどちらも指定されていない場合、 ASC で並び替えられます。
SQLite でランダムにデータを抽出する
まず、SQLite でランダムにデータを取り出す方法
SELECT * FROM entry ORDER BY RANDOM();
SQLite で行数指定(LIMIT)でデータを抽出する
SELECT文では、WHERE句などを指定した場合でも条件に一致する全てのデータを取得します。この取得する件数を制限したい場合にはLIMIT句を使います。書式は以下の通りです。
SELECT カラム名, ... FROM テーブル名 LIMIT 行数;
…
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @”Cell”; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } //ref of Addressbook ABAddressBookRef book = ABAddressBookCreate(); //Count of Addressbook //CFIndex cnt = ABAddressBookGetPersonCount(book); //NSLog(@”addressbook count is %d “,cnt); //AllRecords of Addressbook CFArrayRef records = ABAddressBookCopyArrayOfAllPeople(book); //record at index=i ABRecordRef person = CFArrayGetValueAtIndex(records,indexPath.row); NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); if (firstName == nil) { firstName = @””; } if (lastName == nil) { lastName = @””; } cell.textLabel.text = [NSString stringWithFormat:@”%@ %@”,lastName,firstName]; return cell; }
graffiti on the web
この投稿へのコメント
コメントはまだありません。