2013-08-17 81 views
0

我有一個sqlite數據庫中有一個日期變量的表。我已成立了,告訴我的日期是當天在這段代碼有什麼功能:從數據庫中選擇日期=今天

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
self.dateFormatter = [[NSDateFormatter alloc] init]; 
[self.dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
[self.dateFormatter setTimeStyle:NSDateFormatterNoStyle]; 
_todayDate = [self.dateFormatter stringFromDate:[NSDate date]]; 

我再製作一個使用日期的SQL語句:

-(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); 
} 

然後最後調用函數:

NSString *filePath = [self copyDatabaseToDocuments]; 
[self readRecordsFromDatabaseWithPath:filePath]; 
[self.tableView reloadData]; 

我對代碼的大量對不起,雖然我很新的Objective-C和通常人們希望看到一些代碼。

+0

而你的問題是什麼?無論如何,可能的答案是日期值的格式不匹配。 –

+0

你是否想要所有記錄的日期是「今天」?如果是的話,請看我的答案 –

+0

@ ollie-cole此評論與你的問題無關。我建議你爲所有數據庫操作使用[FMDB Framework](https://github.com/ccgus/fmdb)。你可以參考這個[答案](http://stackoverflow.com/a/17268032/1017893) – icodebuster

回答

0

我發現瞭如何做我自己,然後檢查了每個日期,如果它等於一個字符串,如果它是然後將其添加到數組

0

上面的代碼將會給你當前的日期

NSDate* date = [NSDate date]; 
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; 
    NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone]; 
    formatter.timeZone = destinationTimeZone; 
    [formatter setDateStyle:NSDateFormatterLongStyle]; 
    [formatter setDateFormat:@"dd/MM/yyyy"]; 
    NSString* dateString = [formatter stringFromDate:date]; 

    NSLog(@"tha date is..%@",dateString); 

,你的SELECT查詢會是這樣

NSString *queryString = [NSString stringWithFormat:@"select * from records where date ='%@'", dateString]; 
const char *sqlStateMent = [queryString UTF8String]; 
+0

我看到你的去向和跟隨它,但它什麼也沒有。我nslogged的sqlstatement然後複製到終端運行它然後它想出了一些結果,這真的很奇怪,你可能會認爲什麼是錯的? – Hive7

相關問題