1

我将日期(以 yyyy-MM-dd 格式)作为字符串存储在数据库中。

现在我想从数据库中获取所有记录,只要日期大于今天的日期。

我也尝试使用铸造来做到这一点,但仍然没有为我工作。

SELECT * FROM TableName WHERE CAST(valiDate AS DATETIME) >= ('2103-09-16').

让我知道,以防我在这里做错了什么。

非常感谢任何解决方案。

4

4 回答 4

1

尝试这个

   NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    NSString *s_tody=[dateformatter stringFromDate:[NSDate date]];

    [self databaseOpen];
    NSArray *temp=[[NSArray alloc]init];
    NSString *s=[NSString stringWithFormat:@"select * from Task WHERE date > '%@'",s_tody];
于 2013-09-16T10:22:52.037 回答
1

如果你想要数据库查询,你最好使用 NSDate。SQLite 将日期存储为自 1970 年 1 月 1 日以来的秒数,因此比较非常有效。使用字符串,您可以进行非常昂贵的字符串比较。

您可以像这样在数据库中存储日期:

float dateToStore = [theDate timeIntervalSince1970];

您可以像这样检索它:

NSDate *retrievedDate = [NSDate dateWithTimeIntervalSince1970:numberFromDB];

您可以像这样过滤查询结果:

float dateToQuery = [theDate timeIntervalSince1970];
NSString *query = [NSString stringWithFormat:
        @"SELECT * FROM t1 WHERE t1.date > %@", dateToQuery];
于 2013-09-16T10:33:37.430 回答
0

首先,您将使用以下代码获取系统的日期.....

NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyy-MM-dd"];
NSString *currentdate=[dateformatter stringFromDate:[NSDate date]];

现在您可以使用以下几行将您的当前日期与您的数据库存储进行比较。

数据库代码。

    databaseName = @"youdatabase.sqlite";

documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

documentsDir = documentPaths[0];

databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

   if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
    const char *sqlStatement = [NSString stringWithFormat:@"select * from table WHERE date > '%@'",currentdate];;
}
于 2013-09-16T10:42:16.110 回答
0

将 utcStr 转换为日期

+(NSDate*) dateWithUTCStr :(NSString *)utcDateStr{

    double timeInterval = [utcDateStr doubleValue];
    timeInterval = timeInterval / 1000;

    return [NSDate dateWithTimeIntervalSince1970:timeInterval];
}

将日期转换为 UTC

+(NSTimeInterval) UTCWithDate:(NSDate *)date{

    return [date timeIntervalSince1970]*1000;
}

享受编码...

于 2013-09-16T10:44:17.100 回答