2

我想通过查询获取我组的数据并将它们加载到文本字段中。这是我的表整数 ID 名称文本持续时间实时实时名称列中的名称很少我尝试为每个名称调用方法,但它导致插入失败。

我想收到这个查询并将结果加载到特定标签上:我不知道如何获得响应

SELECT  Name,SUM (time) FROM  cost GROUP BY Name;

有我当前的代码

 NSString *docsDir;
NSArray *dirPaths;

 double totaltime = 1.0;

// 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:
                                 @"Database.db"]];


NSString *queryString=@"SELECT SUM(DURATION) AS TOTAL FROM RECORDS";
sqlite3_stmt    *statement;

if(sqlite3_open([databasePath UTF8String], &myDatabase) == SQLITE_OK) {

    if(sqlite3_prepare_v2(myDatabase, [queryString UTF8String], -1, &statement, nil) == SQLITE_OK){
        while (sqlite3_step(statement) == SQLITE_ROW) {
             totaltime =  sqlite3_column_double(statement, 0);
            NSLog(@"The sum is  %f ", totaltime);
        }
    }
}
integer_t intotal=totaltime;
NSString* total=[NSString stringWithFormat:@"%d min",intotal];
totaltimefield.text=total;
4

1 回答 1

2

文档中很好地涵盖了这一切,但简而言之,一旦您将 SQL 更新为具有两列:

const unsigned char* name = sqlite3_column_text(statement, 0);
double totaltime =  sqlite3_column_double(statement, 1);
NSLog(@"The sum for %s is  %f ", name, totaltime);

函数中的第二个参数sqlite3_column_x是列号。

于 2013-07-25T15:23:14.560 回答