Select from database where date = today
I have a table in an sqlite database which has a variable of date. I have
set up a function that tells me what the date is on that day in this code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
self.dateFormatter = [[NSDateFormatter alloc] init];
[self.dateFormatter setDateStyle:NSDateFormatterShortStyle];
[self.dateFormatter setTimeStyle:NSDateFormatterNoStyle];
_todayDate = [self.dateFormatter stringFromDate:[NSDate date]];
I then make an sql statement that uses the date:
-(void) readRecordsFromDatabaseWithPath:(NSString *)filePath {
sqlite3 *database;
if (sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStateMent = "select * from records where date = ?";
sqlite3_stmt *compiledStatement;
NSLog(@"%d", sqlite3_prepare(database, sqlStateMent, -1,
&compiledStatement, NULL));
if (sqlite3_prepare_v2(database, sqlStateMent, -1,
&compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_text(compiledStatement, 1, [_todayDate
UTF8String], -1, SQLITE_TRANSIENT);
while (sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSString *recordid = [NSString stringWithUTF8String:(char
*)sqlite3_column_text(compiledStatement, 0)];
NSString *patientid = [NSString stringWithUTF8String:(char
*)sqlite3_column_text(compiledStatement, 1)];
NSString *treatmentid = [NSString
stringWithUTF8String:(char
*)sqlite3_column_text(compiledStatement, 2)];
NSString *date = [NSString stringWithUTF8String:(char
*)sqlite3_column_text(compiledStatement, 3)];
NSString *name = [NSString stringWithUTF8String:(char
*)sqlite3_column_text(compiledStatement, 4)];
NSString *area = [NSString stringWithUTF8String:(char
*)sqlite3_column_text(compiledStatement, 5)];
NSString *level = [NSString stringWithUTF8String:(char
*)sqlite3_column_text(compiledStatement, 6)];
NSString *shots = [NSString stringWithUTF8String:(char
*)sqlite3_column_text(compiledStatement, 7)];
Record *newRecord = [[Record alloc] init];
newRecord.patientid = patientid;
newRecord.recordid = recordid;
newRecord.treatmentid = treatmentid;
newRecord.date = date;
newRecord.name = name;
newRecord.area = area;
newRecord.level = level;
newRecord.shots = shots;
LSAppDelegate *delegate = (LSAppDelegate *)[[UIApplication
sharedApplication] delegate];
[delegate.records addObject:newRecord];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
Then finally call the function:
NSString *filePath = [self copyDatabaseToDocuments];
[self readRecordsFromDatabaseWithPath:filePath];
[self.tableView reloadData];
I'm sorry for the vast amount of code though I am quite new to objective-c
and normally people want to see some code.
No comments:
Post a Comment