Reputation: 97
I want to get data of my group by query and load them into textfields. here is my table integer ID Name text Duration Real Time Real there are few names in in Name column I tried calling method for each name , but it caused insert failure.
I want receive this query and load the result on specific Label: i dont know how can get the response
SELECT Name,SUM (time) FROM cost GROUP BY Name;
there is my current code
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;
Upvotes: 2
Views: 859
Reputation: 52565
This is all covered pretty nicely in the documentation, but, in short, once you update your SQL to have two columns:
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);
The second parameter int the sqlite3_column_x
functions is the column number.
Upvotes: 2