2

我编写了一个函数来从在 sqlite 中创建的表中查找总费用和今天的费用。这是我的代码

-(void) calculateTodaysExp    
{
    const char *dbpath = [databasePath UTF8String];    
    sqlite3_stmt *statement1;    
    if(sqlite3_open(dbpath, &expenseDB) == SQLITE_OK)    
    {
        NSString *todays = [NSString stringWithFormat:@"SELECT sum(amount) FROM expenseDetails WHERE date = \"%@\"", dateString];    
        const char *todays_statement = [todays UTF8String];    
        if(sqlite3_prepare_v2(expenseDB, todays_statement, -1, &statement1, NULL) == SQLITE_OK)    
        {
            while (sqlite3_step(statement1) == SQLITE_ROW)    
            {
                NSString *todaysExpenseString = [[NSString alloc]initWithUTF8String:    (const char *)sqlite3_column_int(statement1, 0)];    
                todaysExpense.text = todaysExpenseString;     

            }
        }
        sqlite3_finalize(statement1);    
        sqlite3_close(expenseDB);    
    }
}

-(void) calculateTotalExp    
{
    const char *dbpath = [databasePath UTF8String];    
    sqlite3_stmt *statement2;    
    if(sqlite3_open(dbpath, &expenseDB) == SQLITE_OK)    
    {
        NSString *total = [NSString stringWithFormat:@"SELECT SUM(amount) FROM expenseDetails"];    

        const char *total_statement = [total UTF8String];    
        if(sqlite3_prepare_v2(expenseDB, total_statement, -1, &statement2, NULL) == SQLITE_OK)    
        {
            while(sqlite3_step(statement2) == SQLITE_ROW)    
            {
                NSString *totalExpenseString = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_int64(statement2, 0)];    
                totalExpense.text = totalExpenseString;    
            }
        }
        sqlite3_finalize(statement2);    
        sqlite3_close(expenseDB);    
    }
}

无论我尝试调用这些函数,程序都会停止并显示“程序接收到的信号:”EXC_BAD_ACCESS“”。如果有人能找到解决办法。提前致谢

这是我的整个代码

#import "sqliteViewController.h"

@implementation sqliteViewController
@synthesize selectCategory = _selectCategory;
@synthesize enterExpense;
@synthesize dateString;
@synthesize showDate;
@synthesize moneyLeft;
@synthesize todaysExpense;
@synthesize totalExpense;
-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

-(void)viewDidLoad
{
    NSString *docsDirectory;
    NSArray *directoryPaths;
    directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDirectory = [directoryPaths objectAtIndex:0];
    databasePath = [[NSString alloc]initWithString:[docsDirectory stringByAppendingPathComponent:@"expense.db"]];
    NSFileManager *fileMan = [NSFileManager defaultManager];
    if([fileMan fileExistsAtPath:databasePath] == NO)
    {
        const char *dbpath = [databasePath UTF8String];
        if(sqlite3_open(dbpath, &expenseDB) == SQLITE_OK)
        {
            char *errorMessage;
            const char *sqlStatement = "create table if not exists expenseDetails (ID INTEGER PRIMARY KEY AUTOINCREMENT, CATEGORY TEXT, AMOUNT FLOAT, DATE TEXT)";
            if (sqlite3_exec(expenseDB, sqlStatement, NULL, NULL, &errorMessage) != SQLITE_OK)
            {
                NSLog(@"failed to create table");
            }
            sqlite3_close(expenseDB);
        }
        else{
            NSLog(@"failed to open/create");
        }
    }

    [super viewDidLoad];
    date = [NSDate date];
    NSDateFormatter *dateFormat = [[ NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"dd/MM/YY"];
    dateString = [dateFormat stringFromDate:date];
    NSLog(@"date%@", dateString);
    showDate.text = dateString;
    [self calculateTodaysExp];
    [self calculateTotalExp];
}

-(void)viewDidUnload
{
    [super viewDidUnload];

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:   (UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(IBAction)categorySave:(id)sender {
    _selectCategory = [[UIActionSheet alloc]initWithTitle:@"select" delegate:self cancelButtonTitle:@"close" destructiveButtonTitle:nil otherButtonTitles:@"food",@"transportation",@"fuel",@"mobile",@"shopping", nil];
    [_selectCategory showInView:self.view];
}
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:  (NSInteger)buttonIndex;
{
    if(buttonIndex == 0)
    {
        categoryBuffer = @"food";
    }
    if(buttonIndex == 1)
    {
        categoryBuffer = @"transportation";
    }
    if(buttonIndex == 2)
    {
        categoryBuffer = @"fuel";
    }
    if(buttonIndex == 3)
    {
        categoryBuffer = @"mobile";
    }
    if(buttonIndex == 4)
    {
        categoryBuffer = @"shopping";
    }
    sqlite3_stmt *statement;
    const char *dbpath = [databasePath UTF8String];
    if(sqlite3_open(dbpath, &expenseDB) == SQLITE_OK)
    {
        NSString *insertExpense = [NSString stringWithFormat:@"INSERT INTO expenseDetails (category, amount, date) Values (\"%@\", \"%d\", \"%@\")", categoryBuffer, enterExpense.text, dateString];
        const char *insert_statement = [insertExpense UTF8String];
        sqlite3_prepare_v2(expenseDB, insert_statement, -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            enterExpense.text = @"";
            [self calculateTodaysExp];
            [self calculateTotalExp];
        }
        else {
            NSLog(@"failed");
        }

        sqlite3_finalize(statement);
        sqlite3_close(expenseDB);
    }
    else
    {
        NSLog(@"failed to insert in table");
    }
}
-(void) calculateTodaysExp
{
    const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt *statement1;
    if(sqlite3_open(dbpath, &expenseDB) == SQLITE_OK)
    {
        NSString *todays = [NSString stringWithFormat:@"SELECT TOTAL(amount) FROM expenseDetails WHERE date = \"%@\"", dateString];
        const char *todays_statement = [todays UTF8String];
        if(sqlite3_prepare_v2(expenseDB, todays_statement, -1, &statement1, NULL) == SQLITE_OK)
        {
            while (sqlite3_step(statement1) == SQLITE_ROW)
            {
                int result = sqlite3_column_int(statement1,0);
                NSString *todaysExpenseString = [NSString stringWithFormat:@"%d", result];
                todaysExpense.text = [NSString stringWithFormat:@"%d", result];
            }
            sqlite3_finalize(statement1);
        }
        else
        {
            NSLog(@"failed to select");
        }
        sqlite3_close(expenseDB);
    }
}
-(void) calculateTotalExp
{
    const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt *statement2;
    if(sqlite3_open(dbpath, &expenseDB) == SQLITE_OK)
    {
        NSString *total = [NSString stringWithFormat:@"SELECT SUM(amount) FROM expenseDetails"];

        const char *total_statement = [total UTF8String];
        if(sqlite3_prepare_v2(expenseDB, total_statement, -1, &statement2, NULL) == SQLITE_OK)
        {
            while(sqlite3_step(statement2) == SQLITE_ROW)
            {
                int result1 = sqlite3_column_int(statement2,0);
                NSString *totalExpenseString = [NSString stringWithFormat:@"%d", result1];
                totalExpense.text = totalExpenseString;
            }
            sqlite3_finalize(statement2);
        }
        sqlite3_close(expenseDB);
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [enterExpense resignFirstResponder];
}
@end
4

1 回答 1

3

您正在调用sqlite3_column_int并创建一个字符串,例如:

NSString *todaysExpenseString = [[NSString alloc]initWithUTF8String:    (const char *)sqlite3_column_int(statement1, 0)];    

那是无效的。您将其转换为(const char *),但它不是字符串,而是整数。你真的需要:

int result = sqlite3_column_int(statement1, 0);
NSString *todaysExpenseString = [NSString stringWithFormat:@"%d", result];
于 2013-04-02T16:34:47.850 回答