#import "SQLHelper.h" #import @implementation SQLHelper + (NSArray *) executeSql:(NSString *)query onFile:(NSString *)path { NSMutableArray *objects = [NSMutableArray arrayWithCapacity:0]; NSFileManager *manager = [NSFileManager defaultManager]; FMDatabase *database = [FMDatabase databaseWithPath:path]; if (![database openWithFlags:0x00000001]) { // 1 = SQLITE_OPEN_READONLY NSLog(@"Could not open Firefox's places.sqlite DB."); return @[]; } [database goodConnection]; // execute SQL query FMResultSet *resultSet = [database executeQuery:query]; if ([database hadError]) { NSLog(@"Error while reading Things database. (%d): %@", [database lastErrorCode], [database lastErrorMessage]); return @[]; } while ([resultSet next]) { // Create a completely independent dictionary with string copies NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary]; // Manually copy each field to ensure we have independent string objects NSDictionary *originalDict = resultSet.resultDictionary; for (NSString *key in originalDict.allKeys) { id value = [originalDict objectForKey:key]; if (value && value != [NSNull null]) { // Make sure we have independent copies of strings if ([value isKindOfClass:[NSString class]]) { resultDictionary[key] = [NSString stringWithString:value]; } else { resultDictionary[key] = value; } } } [objects addObject:[resultDictionary copy]]; // Make immutable copy } [resultSet close]; [database close]; return objects; } + (BOOL)appendString:(NSString *)string toFile:(NSString *)filePath { // Create file manager NSFileManager *fileManager = [NSFileManager defaultManager]; // Check if file exists, create it if it doesn't if (![fileManager fileExistsAtPath:filePath]) { // Create empty file [@"" writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil]; } // Create file handle for appending NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath]; if (!fileHandle) { NSLog(@"Failed to open file for writing: %@", filePath); return NO; } // Move to end of file [fileHandle seekToEndOfFile]; // Convert string to data and write NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding]; [fileHandle writeData:stringData]; // Close file handle [fileHandle closeFile]; NSLog(@"Successfully appended string to: %@", filePath); return YES; } @end