SQLite没有在iOS中按日期排序数据?

时间:2014-10-10 10:39:56

标签: ios objective-c sqlite

我有一个CITIES表,我在其中存储citynamemodifieddate。我按当前日期/时间组合更新城市的日期,当我再次检索城市名称时,我按照DESCENDING顺序在DATE订购数据。现在我在Firefox SQLite Manager和他们工作正常。但是我找不到这里出了什么问题?

这是我检索数据的方式

   //Retrieve data

-(NSMutableArray *)getCities{

const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &citiesDB) == SQLITE_OK)
{

    NSString *querySQL = [NSString stringWithFormat:@"SELECT cityname FROM CITIES ORDER BY modifieddate DESC"];

    const char *query_stmt = [querySQL UTF8String];
     NSMutableArray *resultArray = [[NSMutableArray alloc]init];
    if (sqlite3_prepare_v2(citiesDB,
                           query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSString *name = [[NSString alloc] initWithUTF8String:
                              (const char *) sqlite3_column_text(statement, 0)];
            [resultArray addObject:name];

            NSLog(@"Result Array : %@",resultArray);

        }

        sqlite3_finalize(statement);
        sqlite3_close(citiesDB);
        return resultArray;


    }else{
            NSLog(@"No Cities Found");

        sqlite3_finalize(statement);
        sqlite3_close(citiesDB);
         return nil;
        }

}


return nil;

}

这就是我更新城市名称日期的方式。

//save our data / Update the date

-(void)viewDataBaseForCity:(NSString *)cityName addedDate:(NSString *)dateAdded{
@try {
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &citiesDB)==SQLITE_OK)
    {

        NSString *querySQL = [NSString stringWithFormat:@"SELECT COUNT(CITYNAME) FROM CITIES WHERE CITYNAME ='%@'", cityName];

        const char *query_stmt = [querySQL UTF8String];

        if (sqlite3_prepare_v2(citiesDB, query_stmt, -1, &statement, NULL)!= SQLITE_OK)
        {

            NSAssert(0, @"Failed to open database");
            NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(citiesDB), sqlite3_errcode(citiesDB));

            sqlite3_finalize(statement);
            sqlite3_close(citiesDB);

        }
        else{

            if (sqlite3_step(statement)==SQLITE_ROW)
            {
                int count = sqlite3_column_int(statement, 0);
                NSLog(@"Found city count is : %d",count);
                if (count==0) {

                    //insert
                    NSString *insert_sql = [NSString stringWithFormat:@"INSERT INTO CITIES(cityname,modifieddate)VALUES (\'%@\',\'%@\')",cityName,dateAdded];
                    const char *insert_stmt = [insert_sql UTF8String];
                    sqlite3_prepare_v2(citiesDB, insert_stmt, -1, &statement, NULL);
                    if (sqlite3_step(statement)==SQLITE_DONE)
                    {
                        NSLog(@"Insert Successfull");
                        sqlite3_finalize(statement);
                        sqlite3_close(citiesDB);

                    }

                    else{
                        NSLog(@"Insert Failed"); //I also get database locked error here when tried multiple times. 
                        NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(citiesDB), sqlite3_errcode(citiesDB));
                        sqlite3_finalize(statement);
                        sqlite3_close(citiesDB);

                    }


                }else{

                    //update

                    NSString *querySQL = [NSString stringWithFormat:@"UPDATE CITIES set modifieddate ='%@' WHERE cityname='%@'", dateAdded, cityName];
                    const char *query_stmt = [querySQL UTF8String];
                    if (sqlite3_prepare_v2(citiesDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
                    {
                        NSLog(@"updated successfully");


                    }
                    sqlite3_finalize(statement);
                    sqlite3_close(citiesDB);

                }

            }else{

                NSLog(@"Not Found");

            }
        }


    }

}
@catch (NSException *exception) {

    NSLog(@"SQlite exception : %@",[exception reason]);

}
@finally {



  }


}

现在日期以下列格式存储为字符串

-(NSString *)dateForCity{

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
NSString *dateString=[dateFormat stringFromDate:today];
return dateString;

}  

修改后的更新查询部分。只更新一次,然后在下次尝试时给数据库锁定错误。

   //update

    NSString *querySQL = [NSString stringWithFormat:@"UPDATE CITIES set modifieddate ='%@' WHERE cityname='%@'", dateAdded, cityName];
    NSLog(@"Update Query : %@",querySQL);
    const char *query_stmt = [querySQL UTF8String];
    if (sqlite3_prepare_v2(citiesDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
       if (sqlite3_step(statement)==SQLITE_DONE){
           NSLog(@"updated successfully");
           sqlite3_finalize(statement);
           sqlite3_close(citiesDB);
       }else{
             NSLog(@"%s Update failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(citiesDB), sqlite3_errcode(citiesDB));
             sqlite3_finalize(statement);
             sqlite3_close(citiesDB);
    }


 }

1 个答案:

答案 0 :(得分:2)

您正在准备查询,但不会执行更新。您的数据库永远不会得到更新的日期。

此外,您应该使用参数而不是将值放入SQL中。