Objective-C 和 sqlite 的 DATETIME 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/302664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-C and sqlite's DATETIME type
提问by jerodsanto
I have a sqlite3 table that I'm trying to map to an object in objective-C. One attribute of the table is 'completed_at' which is stored as a DATETIME.
我有一个 sqlite3 表,我试图将它映射到 Objective-C 中的一个对象。该表的一个属性是“completed_at”,它存储为 DATETIME。
I want to create a property on my objective-C class (which inherits from NSObject) that will map well to the 'completed_at' attribute.
我想在我的 Objective-C 类(它继承自 NSObject)上创建一个属性,该属性将很好地映射到 'completed_at' 属性。
Objective-C has an NSDate type but I'm not sure if that will map directly?
Objective-C 有一个 NSDate 类型,但我不确定它是否会直接映射?
回答by Hussain KMR Behestee
I am sharing here just the core things regarding date formatting for saving and retrieving the data for presentation. If you have any problem with this code snippet then I will share the full code that I used for my project.
我在这里只分享有关保存和检索演示数据的日期格式的核心内容。如果您对此代码片段有任何问题,那么我将分享我用于我的项目的完整代码。
When you save your data, bind your date value in the sql statement like this way:
当你保存你的数据时,像这样在 sql 语句中绑定你的日期值:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString=[dateFormat stringFromDate:[NSDate date]];
sqlite3_bind_text(saveStmt, 1, [dateString UTF8String] , -1, SQLITE_TRANSIENT);
and when you retrieve data you have to write this code:
当您检索数据时,您必须编写以下代码:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *myDate =[dateFormat dateFromString:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]];
now you have a variable myDate of NSDate type which you can render in your way:
现在你有一个 NSDate 类型的变量 myDate ,你可以用你的方式呈现:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy hh:mm:ss a"];
NSLog(@"My Date was : %@", [formatter stringFromDate:myDate]);
You must have to remember three things:
你必须记住三件事:
- In your SQLite date field type should be DATETIME
- Date format should be same when you store and when you retrieve
- Now you can show in your own way but following the format. Below the format details is given.
- 在您的 SQLite 日期字段中,类型应为 DATETIME
- 存储和检索时的日期格式应相同
- 现在您可以按照自己的方式展示,但要遵循格式。下面给出了格式的详细信息。
Format:
格式:
'dd' = Day 01-31
'MM' = Month 01-12
'yyyy' = Year 2000
'HH' = Hour in 24 hour
'hh' = Hour in 12 hour
'mm' = Minute 00-59
'ss' = Second 00-59
'a' = AM / PM
回答by converter42
I have zero experience with Objective-C, but I found Apple's NSDate Class Referencewith a google search. With the information provided on the linked page you should be able to figure out how to manipulate 32-bit epoch times in Objective-C, and this would work well in SQLite. I would probably create the completed_at column as type INTEGER for 32-bit times.
我对 Objective-C 的经验为零,但我通过谷歌搜索找到了 Apple 的NSDate 类参考。通过链接页面上提供的信息,您应该能够弄清楚如何在 Objective-C 中操作 32 位纪元时间,这在 SQLite 中很有效。对于 32 位时间,我可能会将 Completed_at 列创建为 INTEGER 类型。
SQLite really prefers Julian dates, which are floats. I haven't found any documentation explaining how one might coerce the NSDate class into working with Julians.
SQLite 真的更喜欢 Julian 日期,它是浮点数。我还没有找到任何解释如何强制 NSDate 类与 Julians 一起工作的文档。
timeIntervalSince1970looks very interesting.
timeIntervalSince1970看起来很有趣。
回答by Stephen Darlington
回答by mobibob
The formatter is important if you are trying to effect the presentation but if you use if for internal storage, you are defining a string which can defeat the DB-engine's ability to use the value for computation, comparison, sorting, etc. Also, if you are going to have different clients inserting the date value into the DB you would have to write conversion functions everywhere. I used the following and it worked as expected (schema's column defined as DATETIME):
如果您试图影响演示,格式化程序很重要,但如果您将 if 用于内部存储,则您正在定义一个字符串,该字符串可能会破坏数据库引擎使用该值进行计算、比较、排序等的能力。此外,如果您将有不同的客户端将日期值插入到数据库中,您将不得不在任何地方编写转换函数。我使用了以下内容,它按预期工作(模式的列定义为 DATETIME):
dateExpires = [NSDate dateWithTimeIntervalSinceNow: sqlite3_column_double(queryStmt, 5)];
I inserted into the SQLITE3 db with the Firefox add-on as "4/12/2010" here in Central time zone. Viewing the value of 'dateExpires' in XCode-debugger displayed as:
我在中央时区将 Firefox 插件作为“4/12/2010”插入到 SQLITE3 数据库中。查看 XCode-debugger 中 'dateExpires' 的值显示为:
2010-04-12 23:19:48 -0500
Sure enough, that is the correct time.
果然,那是正确的时间。
Also, to insert into the SQLITE DB you will put the value [NSDate date]
此外,要插入 SQLITE DB,您将输入值 [NSDate date]

