Posted By

espinallab on 02/04/10


Tagged

sqlite3


Versions (?)

SQLite3 Sample data


 / Published in: Objective C
 

  1. #pragma mark Sqlite access
  2.  
  3. - (NSString *)databasePath
  4. {
  5. return [[NSBundle mainBundle] pathForResource:@"holidays" ofType:@"db"];
  6. }
  7.  
  8. - (void)loadHolidaysFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate:(id<KalDataSourceCallbacks>)delegate
  9. {
  10. NSLog(@"Fetching holidays from the database between %@ and %@...", fromDate, toDate);
  11. sqlite3 *db;
  12. NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];
  13.  
  14. if(sqlite3_open([[self databasePath] UTF8String], &db) == SQLITE_OK) {
  15. const char *sql = "select name, country, date_of_event from holidays where date_of_event between ? and ?";
  16. sqlite3_stmt *stmt;
  17. if(sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
  18. [fmt setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
  19. sqlite3_bind_text(stmt, 1, [[fmt stringFromDate:fromDate] UTF8String], -1, SQLITE_STATIC);
  20. sqlite3_bind_text(stmt, 2, [[fmt stringFromDate:toDate] UTF8String], -1, SQLITE_STATIC);
  21. [fmt setDateFormat:@"yyyy-MM-dd"];
  22. while(sqlite3_step(stmt) == SQLITE_ROW) {
  23. NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 0)];
  24. NSString *country = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 1)];
  25. NSString *dateAsText = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 2)];
  26. [holidays addObject:[Holiday holidayNamed:name country:country date:[fmt dateFromString:dateAsText]]];
  27. }
  28. }
  29. sqlite3_finalize(stmt);
  30. }
  31. sqlite3_close(db);
  32. [delegate loadedDataSource:self];
  33. }

Report this snippet  

You need to login to post a comment.