Ios 在xcode中对SQL数据进行排序

Ios 在xcode中对SQL数据进行排序,ios,sql,sorting,Ios,Sql,Sorting,我正在尝试对iOS应用程序中的SQL数据进行排序/排序。数据库运行良好。我可以添加数据,在tableview中显示,编辑,删除一切工作正常。只是无法分类。下面是我正在使用的代码 -(void)sortTable { //CHECKING DIRECTORY AND CREATING DATABASE CODE STARTS NSString *docsDir; NSArray *dirPaths; // Get the documents di

我正在尝试对iOS应用程序中的SQL数据进行排序/排序。数据库运行良好。我可以添加数据,在tableview中显示,编辑,删除一切工作正常。只是无法分类。下面是我正在使用的代码

    -(void)sortTable
    {
    //CHECKING DIRECTORY AND CREATING DATABASE CODE STARTS

    NSString *docsDir;
    NSArray *dirPaths;

    // Get the documents directory

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = dirPaths [0];

    //Build the path to the database file
    _databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"events.db"]];

    NSFileManager *filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: _databasePath] == NO)
    {
        const char *dbpath = [_databasePath UTF8String];

        if (sqlite3_open(dbpath, &_eventDB) == SQLITE_OK)
        {
            char *errMsg;
            const char *sql_stmt =
            "CREATE TABLE IF NOT EXISTS EVENTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, EVENT TEXT, EVENTDATE TEXT)";

            //closing database after creating
            if (sqlite3_exec(_eventDB, sql_stmt, NULL, NULL, &errMsg) !=SQLITE_OK)
            {
                //  sqlite3_close(_eventDB);
            }
        }
    }
    //CHECKING DIRECTORY AND CREATING DATABASE CODE ENDS




   //sort sql table

        sqlite3_stmt *statement;
        const char *dbpath = [_databasePath UTF8String];

       if (sqlite3_open(dbpath, &_eventDB) == SQLITE_OK)
        {
            NSString *sortSQLTable = [NSString stringWithFormat:@"SELECT EVENT FROM EVENTS ORDER BY EVENT"]; // EVENTS is the table name and EVENT is the column.

            const char *sort_stmt = [sortSQLTable UTF8String];
            sqlite3_prepare_v2(_eventDB, sort_stmt, -1, &statement, NULL);
        }

    }
我正在viewDidLoad中执行此void语句

我找了很多。尝试了不同的事情。就是不能让它工作。有人能帮忙吗

更新:

我刚刚发现ORDERBY查询实际上是对表进行排序,但它并没有保存数据库


知道如何在排序后保存数据库吗?

如果您希望对数据进行排序以显示在tableview上,可以使用

但要点是

//array of People objects with properties of firstName and lastName just for example
NSArray *firstNames = @[ @"Jan", @"Steve", @"Jay", @"Mike" ];
NSArray *lastNames = @[ @"Joker", @"Jones", @"Smith", @"Pham" ];

NSMutableArray *people = [NSMutableArray array];
[firstNames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    Person *person = [[Person alloc] init];
    person.firstName = [firstNames objectAtIndex:idx];
    person.lastName = [lastNames objectAtIndex:idx];
    [people addObject:person];
}];

//make the descriptor
NSSortDescriptor *firstNameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"firstName" 
  ascending:YES ];

//sort the array using the descriptor
NSLog(@"By first name: %@", [people sortedArrayUsingDescriptors:@[firstNameSortDescriptor]]);

你能提供更多的细节吗?例如,您使用哪种数据结构来填充数据库数据?感谢您的回复Koray Alkan。我不知道你到底问了什么,我已经用我用来创建表的代码编辑了我的问题。你也可以在将数据加载到表中的地方发布代码并保存它吗?@Koray Alkan,在你询问我将数据加载到表中的代码后,我查看了我的代码并发现了问题。在将数据加载到表中的过程中,我使用了@“从事件中选择*”。当我将代码更改为@“按事件顺序从事件中选择*”时,它就开始工作了。实际上,排序后我不必保存表,只需显示它。非常感谢你提供的线索。你救了我一天:-)很高兴你找到了它!-)谢谢你的回复,艾丽西亚·卡诺。仅对tableView排序会导致更多问题。当我点击编辑单元格数据时,实际上是在编辑sqlite数据库中的另一行,因为tableView中的表的顺序与SQL数据库中的表的顺序不同。