Jaffe的分享空間 Rotating Header Image

Xcode NSDateFormatter + SQLite 迷思

雖然小弟係Xcode既新手,但都話晒寫左program好多年,今日竟然被呢個看似小小既問題困擾左成晚,由夜晚兩點幾開始trial and error, 到而家六點天光,仲未成功解決!

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@”yyyy-MM-dd HH:mm:ss ZZZ”];
[dateFormatter setLocale:[NSLocale currentLocale]];

跟住用句咁既select statement insert入SQLite:

querySQL = [NSString stringWithFormat:@”INSERT INTO table (test_date) values (‘%@’)”, [dateFormatter stringFromDate:testDate]];
const char *query_stmt = [querySQL UTF8String];
sqlite3_prepare_v2(myDB, query_stmt, -1, &statement, NULL)

–> testDate xcode datatype係NSDate, test_date SQLite datatype係datetime <–

從SQLite select返出黎之後, 竟然變左null!百思不得其解…

querySQL = @”SELECT test_date FROM table”;
const char *query_stmt = [querySQL UTF8String];
sqlite3_prepare_v2(myDB, query_stmt, -1, &statement, NULL)

testDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];

就咁NSLog(@”%@”, testDate); 會睇到: 2012-01-06 05:06:36 +0800

但如果NSLog(@”%@”, [dateFormatter stringFromDate:testDate]); 就會變左(null)囉!救命啊!

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: