Created
March 1, 2012 12:10
-
-
Save FlaviuSim/1949450 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "DrugFetcher.h" | |
#import "/usr/include/sqlite3.h" | |
#import "Drug.h" | |
@implementation DrugFetcher | |
#define DATABASE_NAME @"drug_list.sqlite3" | |
+ (void) populateDrugsInManagedObjectContext:(NSManagedObjectContext *)context | |
{ | |
NSLog(@"Begin Drug Fetching from sqlite db"); | |
//use the bundle path because that's where we put the sqlite3 resource | |
NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; | |
NSString *dbFilePath = [bundlePath stringByAppendingPathComponent:DATABASE_NAME]; | |
NSLog(@"drug_list db file path: %@", dbFilePath); | |
sqlite3 *database; | |
if (sqlite3_open([dbFilePath UTF8String], &database)) { | |
NSLog(@"sqlite3_open: failed"); | |
} else { | |
NSString *nsquery = [[NSString alloc] initWithFormat:@"SELECT field1, field2 FROM drug"]; | |
const char *query = [nsquery UTF8String]; | |
sqlite3_stmt *statement; | |
int prepareCode = (sqlite3_prepare_v2( database, query, -1, &statement, NULL)); | |
if(prepareCode == SQLITE_OK) { | |
Drug *drug; | |
NSLog(@"About to start adding the data"); | |
while (sqlite3_step(statement) == SQLITE_ROW) | |
{ | |
drug = [NSEntityDescription insertNewObjectForEntityForName:@"Drug" inManagedObjectContext:context]; | |
drug.route = [NSString stringWithCString:(char *)sqlite3_column_text(statement, 0) encoding:NSUTF8StringEncoding]; | |
drug.new_dose_form = [NSString stringWithCString:(char *)sqlite3_column_text(statement, 1) encoding:NSUTF8StringEncoding]; | |
} | |
sqlite3_finalize(statement); | |
NSLog(@"Added all the sqlite3 Drug data"); | |
} else { | |
NSLog(@"Prepare code was not right: %d",prepareCode); | |
} | |
NSError *err; | |
NSFileManager *filemgr = [[NSFileManager alloc] init]; | |
[filemgr removeItemAtPath:dbFilePath error:&err]; | |
NSLog(@"Deleted sqlite3 database"); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment