Jesse
Jesse

Reputation: 39

Beginner problem with SQLite UPDATE statement

****Please flag as unnecessary-The question no longer relates to the answer.****

I need help changing a single "cell" in a SQLite database. It's 0, it needs to be 1. (Yes, I'm using ints as bools). I believe the problem is in the update.

+favStmt is set to nil earlier.

+gameID is the primary key column.

+useGameID passes in the primary key for the row I want to update.

+isFavorite is the column I want to update.

- (void) addFavorite:(NSInteger) useGameID{


if(favStmt==nil){
    const char *sql = "update GameTable Set isFavorite = ? Where gameID=?";
    if(sqlite3_prepare_v2(database, sql, -1, &favStmt, NULL) !=SQLITE_OK)
        NSAssert1(0,@"Error while creating favorite stmt. '%s'", sqlite3_errmsg(database));

}
sqlite3_bind_int(favStmt, 1, 1);
sqlite3_bind_int(favStmt, 2, useGameID);

if (SQLITE_DONE != sqlite3_step(favStmt))
    NSAssert1(0, @"Error while favoriting. '%s'", sqlite3_errmsg(database));


sqlite3_reset(favStmt);

}

I found a similar question, but I couldn't figure it out for my purpose. Changing a value in SQLite3

Thank you for anything!

And for those who say only masochists use straight SQLite, two things. One, yes, I'm a masochist. Two, I only need this last statement and I should be done with direct SQLite.

Upvotes: 1

Views: 1471

Answers (1)

mu is too short
mu is too short

Reputation: 434596

Here's a real answer, or at least a better one.

Your sqlite3_prepare_v2, sqlite3_bind_int, sqlite3_step, sqlite3_reset sequence looks correct but I think you're giving sqlite3_bind_int the wrong arguments. The arguments for sqlite3_bind_int are supposed to be:

  1. The statement handle.
  2. The the index of the SQL parameter to be set where indexes start at one.
  3. The value to bind.

So I think your bind should look like this:

sqlite3_bind_int(favStmt, 1, 4);

And there's no reason to apologize for occasionally using low level interfaces either, it is useful to know what's going on underneath all the usual pleasantries sometimes. Just don't make a habit of trying to write everything in assembler so to speak :)

Upvotes: 1

Related Questions