Second query to SQLite (on iPhone) errors.

Posted by Luke on Stack Overflow See other posts from Stack Overflow or by Luke
Published on 2011-01-10T20:55:17Z Indexed on 2011/01/10 21:53 UTC
Read the original article Hit count: 127

Filed under:
|
|

Hi all, On the iPhone, I am developing a class for a cart that connects directly to a database. To view the cart, all items can be pulled from the database, however, it seems that removing them doesn't work. It is surprising to me because the error occurs during connection to the database, except not the second time I connect even after the DB has been closed.

#import "CartDB.h"
#import "CartItem.h"

@implementation CartDB

@synthesize database, databasePath;

- (NSMutableArray *) getAllItems {
NSMutableArray *items = [[NSMutableArray alloc] init];

if([self openDatabase]) {
    const char *sqlStatement = "SELECT * FROM items;";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(cartDatabase, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            int rowId = sqlite3_column_int(compiledStatement, 0);
            int productId = sqlite3_column_int(compiledStatement, 1);
            NSString *features = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
            int quantity = sqlite3_column_int(compiledStatement, 3);

            CartItem *cartItem = [[CartItem alloc] initWithRowId:rowId productId:productId features:features quantity:quantity];

            [items addObject:cartItem];

            [cartItem release];
        }
    }
    sqlite3_finalize(compiledStatement);
}
[self closeDatabase];

return items;
}

- (BOOL) removeCartItem:(CartItem *)item {
sqlite3_stmt *deleteStatement;

[self openDatabase];

const char *sql = "DELETE FROM items WHERE id = ?";
if(sqlite3_prepare_v2(cartDatabase, sql, -1, &deleteStatement, NULL) != SQLITE_OK) {
    return NO;
}

sqlite3_bind_int(deleteStatement, 1, item.rowId);

if(SQLITE_DONE != sqlite3_step(deleteStatement)) {
    sqlite3_reset(deleteStatement);
    [self closeDatabase];
    return NO;
} else {
    sqlite3_reset(deleteStatement);
    [self closeDatabase];
    return YES;
}
}


- (BOOL) openDatabase {
if(sqlite3_open([databasePath UTF8String], &cartDatabase) == SQLITE_OK) {
    return YES;
} else {
    return NO;
}
}

- (void) closeDatabase {
sqlite3_close(cartDatabase);
}

The error occurs on the line where the connection is opened in openDatabase. Any ideas? Need to flush something? Something gets autoreleased? I really can't figure it out.

--Edit--

The error that I receive is GDB: Program received signal "EXC_BAD_ACCESS".

--Edit--

I ended up just connecting in the init and closing in the free methods, which might not be the proper way, but that's another question altogether so it's effectively persistent instead of connecting multiple times. Still would be nice to know what was up with this for future reference.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about sqlite3