I am working with Sqlite DB for storing and retrieving the data and displaying it in the tableview. My question is how to display the data based on latest record entered in the DB.
Let me explain what I am doing.
-
Users will enter there data (fields like date,gender,country) and once clicked on save button the data gets stored in sqlite DB.
-
After saving immediately I will show only the dates in the tableview(next page) that I will be pulling it from the sqlite DB. But when I am displaying the dates in tableview the dates are displayed in ascending order (based on numbers).
-
But I want to display as per latest record entered by the user. below is the what I am doing in coding.
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); }}
What you can do is when inserting the data (entered by the user) into sqlite DB, have another column called timestamp & insert the current timestamp (maybe epoch time). This could be done for each row.
When retrieving you can
order by timestamprather than the ids which is what you want.