FMDBによるデータベース検索
NSURL *dirPath;
FMDatabase *db;
NSURL *dbPath;
//Open DB
dbPath = [dirPath URLByAppendingPathComponent:@"cdb.db"];
db = [FMDatabase databaseWithPath:[dbPath absoluteString]];
[db open];
//Read all
FMResultSet *rs = [db executeQuery:@"select * from cdb"];
ContId[j] = [rs stringForColumn:@"ContId"];
CateId[j] = [rs stringForColumn:@"CateId"];
title[j] = [rs stringForColumn:@"Title"];
imageFile[j] = [rs stringForColumn:@"ImageFile"];
detail1[j] = [rs stringForColumn:@"Detail1"];
}
[rs close];
[db close];
特定の項目検索の場合
FMResultSet *rs = [db executeQuery:@"select * from cdb where CateId = ?", cid];
特定のワードを含む検索の場合
//検索ワード
NSString *srchtxt = [[NSString alloc]init] ;srchtxt = [srchtxt stringByAppendingString:@"%%"];
srchtxt = [srchtxt stringByAppendingString:srchtext.text];
srchtxt = [srchtxt stringByAppendingString:@"%%"];
FMResultSet *rs = [db executeQuery:@"select * from cdb where Title like ?", srchtxt];
データベースの作成
dbPath = [dirPath URLByAppendingPathComponent:@"cdb.db"];
db = [FMDatabase databaseWithPath:[dbPath absoluteString]];
[db open];
[db beginTransaction];
[db executeUpdate:@"delete from cdb"];
[db executeUpdate:@"create table cdb (ContId text, CateId text, Title text, ImageFile text, Detail1 text)"];
[db commit];
データベース書き込み
//Insert DB
[db beginTransaction];
[db beginTransaction];
[db executeUpdate:@"insert into cdb (ContId,CateId,Title,ImageFile,Detail1) values (?,?,?,?,?)",ContId,CateId,Title,ImageFile,Detail1];
[db commit];