开发者

How show last 10 data stored in data base in table view?

开发者 https://www.devze.com 2023-04-04 05:29 出处:网络
I have data base in which i am storing source address,destination address and category. Now i want to show only last 10 data stored in that data base on table view. I am using this code for retrieving

I have data base in which i am storing source address,destination address and category. Now i want to show only last 10 data stored in that data base on table view. I am using this code for retrieving data form data base...

 -(void) openDB {

listOfStudents = [[NSMutableArray alloc] init];
if(sqlite3_open([databasePath UTF8Str开发者_StackOverflow中文版ing], &db) == SQLITE_OK) {
    const char *sql = "select * from Places";
    sqlite3_stmt *stmt;
    int rtnVal = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
    if( rtnVal == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            Places_data *pd_object = [[[Places_data alloc] init] autorelease];
            pd_object.place_id = sqlite3_column_int(stmt, 0);
            pd_object.start_add = [NSString stringWithUTF8String:(char *) sqlite3_column_text(stmt, 1)];
            pd_object.end_add = [NSString stringWithUTF8String:(char *) sqlite3_column_text(stmt, 2)];
            pd_object.strcategroy =  [NSString stringWithUTF8String:(char *) sqlite3_column_text(stmt, 3)];

            [self.listOfStudents addObject:pd_object];
        }
    }
    sqlite3_finalize(stmt);
}
sqlite3_close(db);

}

Now what i do changes in above code show that it show only last 10 results in table view?


you can use following query which will fetch only last 10 records only.

const char *sql = "SELECT * FROM Places order by rowid desc limit 10";


It depends what does it mean "last"? If you have any autoincrement id colunm, then you propably mean "last inserted" or those with largest id. In your case, where you are using sqlite, you could use something like this:

SELECT * FROM Places ORDER BY Id DESC LIMIT 10;

Where Id is your autoincrement column name. The statement will select all entries from table Places, then order it by Id descending and use just first 10 results.

In some other databases you could try also:

SELECT TOP 10 * FROM Places ORDER BY Id DESC;

But note that not all database systems support the TOP clause.

Hope it solves your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消