I am trying to set an index database with CoreData
and FMDB
according to this tutorial (here)
I am beginner in ios, so I may made something wrong when adding
SQLITE_ENABLE_FTS3 SQLITE_ENABLE_FTS3_PARENTHESIS macros.
I added them to Other C Flags.
This is my code:
- (void) setIndexDatabase
{
// Create (or open) our database
NSString *dbPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
dbPath = [dbPath stringByAppendingPathComponent:@"Index.sqlite"];
// Using the FMDatabaseQueue ensures that we don't accidentally talk to our database concurrently from two different threads
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
[db executeUpdate:@"CREATE VIRTUAL TABLE IF NOT EXISTS docs USING fts4(name, contents);"];
}];
//Add content to index
[queue inDatabase:^(FMDatabase *db) {
[db executeUpdate:@"INSERT INTO docs (name, contents) VALUES(?, ?);", @"doc1", @"She sells sea shells by the sea shore."];
}];
//Search
__block NSMutableArray *matches = [NSMutableArray array];
[queue inDatabase:^(FMDatabase *db) {
FMResultSet *resultSet = [db executeQuery:@"SELECT name FROM docs WHERE docs MATCH ?", @"she*"];
while ([resultSet next]) {
[matches addObject:[resultSet stringForColumn:@"name"]];
}
}];
}
Edit:
DB Error: 1 "no such module: fts4"
I am calling this function in app delegate at didFinishLaunchingWithOptions method
This error is happening because of build of SQLite does not include the FTS modules. I tried another approach to overcome it. I removed all files about SQLite and FMDB. And then, I added FMDB to my project by using podfile and it worked successfully.