2010-10-01 120 views
2

我使用以下用于列获得最大值码查找一个列最大值从SQLite数据库

柱无11(装置10没有,在代码)柱-integer的 列名称uniqueColumnSNo 数据类型。

我很困惑dbhanlder使用的语法。

这是代码。 请让我知道,我将如何达到要从指定栏返回的最大价值。 帮助。提前致谢。

+(int)的getMaxColumnSNo {

NSInteger favid; 
int count=0; 
sqlite3 *database; 
NSString *dbpath; 
dbpath = [dbhandler dataFilePath:DbName]; 
if (sqlite3_open([dbpath UTF8String], &database) == SQLITE_OK) 
{ 
    NSString *selectSql = [NSString stringWithFormat:@"SELECT max(uniqueColumnSNo) FROM CustomerFields"]; 
    sqlite3_stmt *statement; 
    if (sqlite3_prepare_v2(database, [selectSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) 
    { 
     while (sqlite3_step(statement) == SQLITE_ROW) 
     { 
      //[result addObject:[[NSMutableDictionary alloc] init]]; 
      favid=(NSInteger)sqlite3_column_int64(statement,10); 
      //favid = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 10)] intValue]; 
      count++; 

     } 
     sqlite3_finalize(statement); 
    } 
    else 
    { 
     NSLog(@"Sql Preparing Error"); 
    } 
    sqlite3_close(database); 
} 
else 
{ 
    NSLog(@"Database not opening"); 
} 
int i; 
if (count!=0) { 
i=favid ; 

} 
else { 
    i=1; 
} 

return i; 
//return favid; 

}

+2

接受您先前问题的一些答案,先..... – CristiC 2010-10-01 08:28:52

回答

2

你需要:

sqlite3_column_int64(statement, 0); 

列号参数是结果集的索引,原始表的不列号。结果集中唯一的列是max(uniqueColumnSNo),因此这是列索引编号0.