這是工作的罰款。
1. 表創建
FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
[db open];
[db executeUpdate:@"create table if not exists UserTbl(eventname text,eventdate date)"];
FMResultSet *results = [db executeQuery:@"SELECT * FROM UserTbl"];
while([results next])
{
ReminderObject *Reminder = [[ReminderObject alloc] init];
Reminder.EventTitleString = [results stringForColumn:@"eventname"];
Reminder.EventDateString = [results stringForColumn:@"eventdate"];
[ReminderListArray addObject:Reminder];
}
[db close];
2. 插入
FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
[db open];
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: @"dd-MM-yyyy"];
NSString *dateString=[dateFormat stringFromDate:today];
dateFormat.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSLog(@".........DateString...%@",dateString);
//NSString *query = [NSString stringWithFormat: @"INSERT INTO user (edited) VALUES (\"%@\")", dateString];
BOOL success = [db executeUpdate:@"insert into UserTbl(eventname,eventdate) values(?,?);",
Reminder.EventTitleString,dateString, nil];
[db close];
寫入查詢的格式是正確的或不? – 2013-02-24 14:56:30
日期如何存儲在數據庫中?什麼格式(yyyy-mm-dd)?另外,日期是一個NSDate? – Ares 2013-02-24 19:17:30
在數據庫中,日期以MM-dd-yy格式存儲,並且該列數據類型爲Date。 – 2013-02-25 02:13:23