简体   繁体   English

SQLite存储,检索和比较DATETIME字段

[英]SQLite storing, retrieving and comparing a DATETIME field

I am really stuck trying to compare dates in SQLite queries in Objective C. Here's what I'm doing: 我真的很难在Objective C中比较SQLite查询中的日期。这就是我正在做的事情:

Storing the date: 存储日期:

This document tells me to use the dateformat specified below, but it doesn't seem right. 本文档告诉我使用下面指定的dateformat,但它似乎不正确。 I tried using yyyy-MM-dd hh:mm:ss without success too though. 我试过使用yyyy-MM-dd hh:mm:ss没有成功。

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-DD HH:MM:SS"];
NSString *dateString=[dateFormat stringFromDate:today];

NSString *query = [NSString stringWithFormat: @"INSERT INTO user (edited) VALUES (\"%@\")", dateString];

Comparing the date 比较日期

I am using a timestamp for this, which I convert to a datetime string 我正在使用时间戳,我将其转换为日期时间字符串

long stamp = [sessie integerForKey:@"stamp"];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-DD HH:MM:SS"];
NSString *dateString = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:stamp]];

sqlite3_stmt *result = [db selectQuery:[NSString stringWithFormat:@"SELECT * FROM user WHERE edited > '%@'", dateString]];

The timestamp is simply generated using [[NSDate date] timeIntervalSince1970] . 使用[[NSDate date] timeIntervalSince1970]生成时间戳。 The problem is that the query won't give the correct results, and I don't even know if the date is stored correctly in the first place. 问题是查询不会给出正确的结果,我甚至不知道日期是否正确存储。 Any help would be greatly appreciated! 任何帮助将不胜感激!

A couple of observations: 几点意见:

  1. For your date string, you do definitely do not want to use YYYY-MM-DD HH:MM:SS . 对于你的日期字符串,你肯定不想使用YYYY-MM-DD HH:MM:SS That will not generate a valid date string. 这不会生成有效的日期字符串。 Using yyyy-MM-dd hh:mm:ss is much closer, but not quite right, either (since you'll use 12-hour hh ). 使用yyyy-MM-dd hh:mm:ss更接近,但也不是很正确(因为你将使用12小时hh )。 Use yyyy-MM-dd HH:mm:ss instead. 使用yyyy-MM-dd HH:mm:ss代替。

  2. This date format, though, does not capture time zone, so, if you store date strings in your SQLite database, you should use UTC (GMT) as discussed in the SQLite Date And Time Functions documentation. 但是,此日期格式不捕获时区,因此,如果在SQLite数据库中存储日期字符串,则应使用UTC(GMT),如SQLite 日期和时间函数文档中所述。

     NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; NSString *dateString = [formatter stringFromDate:today]; 

    As shown above, you probably want to also specify the locale so that the format of the dates will not change depending upon the localization settings of the device. 如上所示,您可能还希望指定locale以便日期的格式不会根据设备的本地化设置而更改。

  3. Note that you might consider using timeIntervalSince1970 to store the date as a REAL value in your database (as opposed to a TEXT ). 请注意,您可以考虑使用timeIntervalSince1970将日期作为REAL值存储在数据库中(而不是TEXT )。 This can be a little more efficient and automatically addresses the UTC issue. 这可以更有效并自动解决UTC问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM