Return to Snippet

Revision: 23332
at February 4, 2010 09:44 by espinallab


Initial Code
#pragma mark Sqlite access

- (NSString *)databasePath
{
  return [[NSBundle mainBundle] pathForResource:@"holidays" ofType:@"db"];
}

- (void)loadHolidaysFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate:(id<KalDataSourceCallbacks>)delegate
{
  NSLog(@"Fetching holidays from the database between %@ and %@...", fromDate, toDate);
	sqlite3 *db;
  NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];
  
	if(sqlite3_open([[self databasePath] UTF8String], &db) == SQLITE_OK) {
		const char *sql = "select name, country, date_of_event from holidays where date_of_event between ? and ?";
		sqlite3_stmt *stmt;
		if(sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
      [fmt setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
      sqlite3_bind_text(stmt, 1, [[fmt stringFromDate:fromDate] UTF8String], -1, SQLITE_STATIC);
      sqlite3_bind_text(stmt, 2, [[fmt stringFromDate:toDate] UTF8String], -1, SQLITE_STATIC);
      [fmt setDateFormat:@"yyyy-MM-dd"];
			while(sqlite3_step(stmt) == SQLITE_ROW) {
				NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 0)];
				NSString *country = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 1)];
        NSString *dateAsText = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 2)];
        [holidays addObject:[Holiday holidayNamed:name country:country date:[fmt dateFromString:dateAsText]]];
			}
		}
		sqlite3_finalize(stmt);
	}
	sqlite3_close(db);
  [delegate loadedDataSource:self];
}

Initial URL


Initial Description


Initial Title
SQLite3 Sample data

Initial Tags


Initial Language
Objective C