我创建了一个sqlite数据库。 'Orders'是一个具有idOrder(int),dateOrder(varchar)和discount(float)的表。在Bill.h中,我将dateBill设置为NSDate,那么如何将当前dateBill保存到表'orders'中?
答案 0 :(得分:2)
这里似乎也涵盖了这个主题。 Persisting Dates to SQLite3 in an iPhone Application
+ (NSDate *) dateWithSQLiteRepresentation: (NSString *) myString;
{
NSAssert3(myString, @"%s: %d; %s; Invalid argument. myString == nil", __FILE__, __LINE__, __PRETTY_FUNCTION__);
return [[self sqlLiteDateFormatter] dateFromString: myString];
}
+ (NSDate *) dateWithSQLiteRepresentation: (NSString *) myString timeZone: (NSString *) myTimeZone;
{
NSString * dateWithTimezone = nil;
NSDate * result = nil;
NSAssert3(myString, @"%s: %d; %s; Invalid argument. myString == nil", __FILE__, __LINE__, __PRETTY_FUNCTION__);
NSAssert3(myTimeZone, @"%s: %d; %s; Invalid argument. myTimeZone == nil", __FILE__, __LINE__, __PRETTY_FUNCTION__);
dateWithTimezone = [[NSString alloc] initWithFormat: @"%@ %@", myString, myTimeZone];
result = [[self sqlLiteDateFormatterWithTimezone] dateFromString: dateWithTimezone];
[dateWithTimezone release];
return result;
}
+ (NSString *) sqlLiteDateFormat;
{
return @"yyyy-MM-dd HH:mm:ss";
}
+ (NSString *) sqlLiteDateFormatWithTimeZone;
{
static NSString * result = nil;
if (!result) {
result = [[NSString alloc] initWithFormat: @"%@ zzz", [self sqlLiteDateFormat]];
}
return result;
}
+ (NSDateFormatter *) sqlLiteDateFormatter;
{
static NSDateFormatter * _result = nil;
if (!_result) {
_result = [[NSDateFormatter alloc] init];
[_result setDateFormat: [self sqlLiteDateFormat]];
}
return _result;
}
+ (NSDateFormatter *) sqlLiteDateFormatterWithTimezone;
{
static NSDateFormatter * _result = nil;
if (!_result) {
_result = [[NSDateFormatter alloc] init];
[_result setDateFormat: [self sqlLiteDateFormatWithTimeZone]];
}
return _result;
}
- (NSString *) sqlLiteDateRepresentation;
{
NSString * result = nil;
result = [[NSDate sqlLiteDateFormatter] stringFromDate: self];
return result;
}
- (NSTimeInterval) unixTime;
{
NSTimeInterval result = [self timeIntervalSince1970];
return result;
}
#define SECONDS_PER_DAY 86400
#define JULIAN_DAY_OF_ZERO_UNIX_TIME 2440587.5
- (NSTimeInterval) julianDay;
{
return [self unixTime]/SECONDS_PER_DAY + JULIAN_DAY_OF_ZERO_UNIX_TIME;
}
+ (NSDate *) dateWithJulianDay: (NSTimeInterval) myTimeInterval
{
NSDate * result = [[NSDate date] timeIntervalSince1970];
return result;
}
答案 1 :(得分:0)
列的数据类型是INTEGER
保存日期:
sqlite_bind_double(compiledStatement,1,[yourdate TimeIntervalSince1970]);
取回日期:
NSDate * gotDate = [NSDate dateWithTimeIntervalSinceNow:sqlite3_column_double(compiledStatement,1)];
希望有所帮助
此致
Kuldeep