【问题标题】:iOS SQLite - Max() FunctioniOS SQLite - Max() 函数
【发布时间】:2012-02-26 20:48:47
【问题描述】:

我的 iOS 应用程序中有一个 SQLite 数据库。我正在尝试使用 SQL 函数 MAX()

- (void) getMaxTime {

if (sqlite3_open([databasePath UTF8String], &timingsDatabase) == SQLITE_OK) 
{
    NSString *maxTimeStatement = [NSString stringWithFormat:@" SELECT max(NUMBERS) FROM TIMINGS"];
    sqlite3_stmt *statement;
    if(sqlite3_prepare_v2(timingsDatabase, [maxTimeStatement UTF8String], -1, &statement, nil) == SQLITE_OK){
        while (sqlite3_step(statement) == SQLITE_ROW) {
            massimo = sqlite3_column_int(statement, 0);

            NSLog(@"The maximun time is %d seconds ", massimo);
        } 
        sqlite3_finalize(statement);
        sqlite3_close(timingsDatabase);     
    }      
}

NUMBERS 列中的所有数字在昏迷后都有 6 位数字。
如果所有数字都 > 10 或所有数字都

但是(示例)当我尝试获取其中的最大值时:

  • 1.339670
  • 2.955537
  • 11.355558

它打印:“最大时间是 2 秒”(而不是 11 秒)!我很难理解为什么。

*编辑

这就是我的数据库的创建方式:

-(void)createDatabase 
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    databasePath = [[NSString alloc]initWithString:[documentsDirectory stringByAppendingPathComponent:@"timings.db"]];

    if ([[NSFileManager defaultManager] fileExistsAtPath:databasePath] == FALSE)
    {
        if (sqlite3_open([databasePath UTF8String], &timingsDatabase) == SQLITE_OK)
        {
            const char *sqlStatement = "CREATE TABLE IF NOT EXISTS TIMINGS (ID INTEGER PRIMARY KEY AUTOINCREMENT, TIMESTAMP TEXT, TIMING TEXT, NUMBERS TEXT)";
            char *error;
            sqlite3_exec(timingsDatabase, sqlStatement, NULL, NULL, &error);
            sqlite3_close(timingsDatabase);
        }
    }
}

这就是时间的存储方式:

-(void)storeTiming 
{
    if (sqlite3_open([databasePath UTF8String], &timingsDatabase) == SQLITE_OK)
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm:ss.SSS"];

        NSString *insertStatement = [NSString stringWithFormat:@"INSERT INTO TIMINGS (TIMESTAMP, TIMING, NUMBERS) VALUES (\"%@\", \"%@\", \"%f\")", [dateFormatter stringFromDate:startDate], stopWatchLabel.text , intervallo] ;


        char *error;
        sqlite3_exec(timingsDatabase, [insertStatement UTF8String], NULL, NULL, &error);
        sqlite3_close(timingsDatabase);
    }
} 

【问题讨论】:

  • 错误的列类型可能是答案,但我不知道如何正确将列类型设置为浮点数。

标签: ios sqlite ios5 xcode4.2


【解决方案1】:

似乎该列的数据类型是字符串...您应该将其更改为浮动以使用 MAX() 函数。

【讨论】:

  • 我怎样才能做出改变?是否应该在创建数据库时完成?
  • 谢谢,我知道如何让它浮动,现在可以使用了。
猜你喜欢
  • 1970-01-01
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
相关资源
最近更新 更多