Getsy
Getsy

Reputation: 4905

iPhone:Problem with updating a column in SQLite database

In my iPhone application, i have included sqlite database with two columns. It is structured like,

CREATE TABLE "MyTable" ("ID" INTEGER PRIMARY KEY  NOT NULL ,"name" VARCHAR,"status" INTEGER)

I am trying to update the second column "status" on the fly through programming. I am trying to use the following query for that.

const char *updateStmt = "UPDATE MyTable SET name=?, status=? Where ID=?";

and,

sqlite3_stmt *addstateCompiledStmt;
if ( sqlite3_prepare_v2(database, updateStmt, -1, &addstateCompiledStmt, NULL) ==SQLITE_OK )
                    {
sqlite3_bind_int(addstateCompiledStmt, 2, statusvalue);
}

But it is not updating to the second column with the new value i pass. Could someone please help me what could be wrong here and what needs to be done?

Thank you.

Upvotes: 0

Views: 1200

Answers (1)

Chetan Bhalara
Chetan Bhalara

Reputation: 10344

You need to bind all the "?" value in sqlite stament.

See the links for reference.
http://staging.icodeblog.com/wp-content/uploads/2008/09/dehydrate1.png http://www.scribd.com/doc/11524402/iPhone-Programming-with-SQLite

or,

if (updateStmt == nil) {

    const char *sql = "update Coffee Set CoffeeName = ?, Price = ? Where CoffeeID = ?";
    if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}

sqlite3_bind_text(updateStmt, 1, [coffeeName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_double(updateStmt, 2, [price doubleValue]);
sqlite3_bind_int(updateStmt, 3, coffeeID);
if(SQLITE_DONE != sqlite3_step(updateStmt))
    NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database));
sqlite3_reset(updateStmt);

Upvotes: 1

Related Questions