【发布时间】:2012-01-07 19:32:11
【问题描述】:
我正在使用 Sqlite DB 来存储和检索数据并将其显示在 tableview 中。我的问题是如何根据数据库中输入的最新记录显示数据。 让我解释一下我在做什么。
用户将在那里输入数据(日期、性别、国家等字段),一旦点击保存按钮,数据就会存储在 sqlite DB 中。
立即保存后,我将仅在 tableview(下一页)中显示我将从 sqlite DB 中提取的日期。但是当我在 tableview 中显示日期时,日期按升序显示(基于数字)。
-
但我想根据用户输入的最新记录显示。下面是我在编码中所做的。
sqlite3_stmt *statement; const char *dbpath = [databasePath UTF8String]; NSString *selectSQL; if(sqlite3_open(dbpath, &contactDB) == SQLITE_OK) { if ([set_user isEqualToString:@"Anonymous"] && [set_pass isEqualToString:@"Anonymous"]) { selectSQL = [NSString stringWithFormat: @"SELECT * from Guest where username = '%@' and password = '%@'", set_user,set_pass]; } else { selectSQL = [NSString stringWithFormat: @"SELECT * from Vitals where username = '%@' and password = '%@'", set_user,set_pass ]; } const char *select_stmt = [selectSQL UTF8String]; if(sqlite3_prepare_v2(contactDB, select_stmt, -1, &statement, NULL) == SQLITE_OK) { while(sqlite3_step(statement) == SQLITE_ROW) { sq_date = [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 1)]; sq_gender = [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 2)]; sq_ethnicity = [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 12)]; [date_array addObject:sq_date]; [gender_array addObject:sq_gender]; [ethnicity addObject:sq_ethnicity]; /*if (![date_array containsObject:sq_date]) { [date_array addObject:sq_date]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"" ascending:NO]; [date_array sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; [sortDescriptor release]; }*/ } sqlite3_finalize(statement); }}
【问题讨论】:
标签: objective-c ios uitableview sqlite ios5