views:

34

answers:

1

I'm attempting to make a simple app that stores and displays flash cards, however I'm having an awful time getting my SQLite to work. The database connection is fine, but when I try an insert, it crashes and gives no indication of what went wrong. This is the code I am using to insert the flash card into the table.

const char *insert = "INSERT OR REPLACE INTO LIST (TERM, DEFINITION) VALUES (?, ?);";
sqlite3_stmt *statement;
sqlite3_prepare_v2(database, insert, -1, &statement, nil);
sqlite3_bind_text(statement, 1, [term UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [def UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_step(statement);
sqlite3_finalize(statement);

I've determined that the bind text method is the weak link with some NSLog() methods I placed earlier. In this example, term and def are NSStrings that do hold the correct value (I know that much for sure). Any help would be appreciated. I haven't quite mastered portable c.

+2  A: 

Not 100% why its not working but I have a few things to suggest:

Get rid of the ; at the end of the insert statement, I've never had it in mine.

Put the prepare and step into an if statement like so

if(sqlite3_prepare_v2(db, sql, -1, &statement, NULL) != SQLITE_OK) {
        NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(db));
}
if (SQLITE_DONE != sqlite3_step(statement)) {
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(db));
}

None of this might help but it might get you closer as to whats wrong.

Also, one thing that might help. When I've been working with SQLite its been an utter pain if I make a change to the db or if I'm not putting correct data in. I've spent a lot of time resetting the iPhone simulator and cleaning the build

Rudiger