My app is for expenses in every month, I have table with name Expense contain 4 columns (type, ExpenseMonth, amount and Name) ,every day I insert data for my expenses now How I can display the Expense for every day in a section in the table view?
e.g : if the date is 2-1-2013 I want to retrieve the expense of this day only and so on for other days.
I write the below query but every section retrieve the same data.
const char *sql ="select Name from Expense where strftime('%m-%d',ExpenseMonth) Group by ExpenseMonth order by ExpenseMonth desc";
This my code after correcting….
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSMutableArray *Dateo= [[NSMutableArray alloc]init];
NSMutableArray *NAMeA= [[NSMutableArray alloc]init ];
if (sqlite3_open([[AppDelegate getDBPath] UTF8String], &database) == SQLITE_OK) {
const char *sql ="select ExpenseMonth,count(*) as day from Expense where strftime('%m',ExpenseMonth)=strftime('%m','now') group by ExpenseMonth order by ExpenseMonth desc";
sqlite3_stmt *selectstm;
int result = sqlite3_prepare_v2(database, sql, -1, &selectstm, NULL);
if( result== SQLITE_OK) {
while(sqlite3_step(selectstm) == SQLITE_ROW) {
NSString *dateAndTime=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstm, 0)];
NSInteger numofday=sqlite3_column_int(selectstm,1);
MaxEntity *DatTime=[[MaxEntity alloc]initWithDate:dateAndTime andnumofDay:numofday];
[Dateo addObject:DatTime];
NSLog(@"Date %@",dateAndTime);
for (NSString *str in Dateo) {
NSString *querySQL =[NSString stringWithFormat:@"select Name, amount from Expense where ExpenseMonth=\"%@\" order by ExpenseMonth desc",dateAndTime];
const char *query_stmt = [querySQL UTF8String];
sqlite3_stmt *selectstmt;
int result = sqlite3_prepare_v2(database, query_stmt, -1, &selectstmt, NULL);
if( result== SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSString *NAMe=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
NSString *Amount=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
MaxEntity *NM=[[MaxEntity alloc]initWithName:NAMe andAmount:Amount];
[NAMeA addObject:NM];
MaxEntity *curItem = [NAMeA objectAtIndex:indexPath.section ]; //Get the model information at row location.
cell.textLabel.text = curItem.name;
NSLog(@"text%@",cell.textLabel.text);
[NAMeA removeLastObject];
[Dateo removeLastObject];
}
}
sqlite3_finalize(selectstmt);
}
}
}
sqlite3_finalize(selectstm);
sqlite3_close(database);
}
return cell;
}
It reads the date and then selects the data depending on the date, after done retrieving the data it return again to the database to retrieve the first date again and throws an exception in this line, Plz can any one help me to stop the select statement when retrieve all date???
MaxEntity *curItem = [NAMeA objectAtIndex:indexPath.section ];
select Name from Expense where strftime('%m-%d',ExpenseMonth) Group by ExpenseMonth order by ExpenseMonth descYour where clause just calculates a string value, it doesn’t compare anything, so it will always return the same results. It needs to be in the format of
where ExpenseMonth = .... So, for a given section/day you would need to set a query like:NSString *sql = [NSString stringWithFormat:@"select Name from Expense where ExpenseMonth = %@ Group by ExpenseMonth order by ExpenseMonth desc", sectionDate];(Sorry, this is in Objective-C, which is what I work in, but should give you the idea of how to do it in C++, which I assume is what you’re working in.)
Basically, loop through dates substituting the date string (sectionDate) into the SQL statement (replaces the “%@”) for each section of the table.