0

I am new to sqlite3 in iOS,I am trying to update the existing record in device table which is as below I am not updating the primary key, though I am updating record based on primary key where deviceAppID is my PK

Here is code which I am trying

      const char *updateStatement ="UPDATE  Device SET deviceGUID=?,testGUID=?,deviceName=?,device=?,deviceOsVersion=?,platform=?,createdDateTime=?,updatedDateTime=?,status=? WHERE deviceAppID=1";

      int success;

      if(sqlite3_prepare_v2(mDatabase, updateStatement, -1, &mStatement, NULL) == SQLITE_OK)
          {
sqlite3_bind_text(mStatement, 2, [device.deviceGUID UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(mStatement, 3, [device.testGUID UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(mStatement, 4, [device.deviceName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(mStatement, 5, [device.device UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(mStatement, 6, [device.deviceOSVersion UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(mStatement, 7, [device.devicePlatform UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(mStatement, 8, [device.createdDateTime UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(mStatement, 9, [device.updatedDateTime UTF8String], -1, SQLITE_TRANSIENT);
 sqlite3_bind_int(mStatement, 10, device.status);

            success = sqlite3_step(mStatement);


            if (success == SQLITE_ERROR) {

                  NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(mDatabase));
            }
          }

      else {

            NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(mDatabase));
      }
      sqlite3_finalize(mStatement);
Sandeep Khade
  • 2,832
  • 3
  • 21
  • 37

1 Answers1

2

Please read the following link (the index is NOT the index in the table, it is the index in the STATEMENT)

read this - found it with google

nabuchodonossor
  • 2,095
  • 20
  • 18
  • Thanks @nabuchodonossor can you please tell me context to above code where I mess up the things. – Sandeep Khade Nov 11 '13 at 13:43
  • Yes. If you go to the provided link, you will find a description how to use the bind method (NOTE: For different data types there are different bind methods (int, date ...). In short: You start with index 0 for the first parameter, index 1 for the second parameter and so on. quit simple. all the other comments may also be important, e.g. are you allowed to update the table, and so on. – nabuchodonossor Nov 11 '13 at 13:47
  • but why index is from 1 while inserting the record can you please tell me. – Sandeep Khade Nov 11 '13 at 13:58
  • The index has nothing to do with a table. The index is the index of the parameter in the sql statement. – nabuchodonossor Nov 11 '13 at 14:12
  • I read the doc (from the link) again and found the information, that the value starts with 1 to a maximum of 999 parameters for one statement. – nabuchodonossor Nov 11 '13 at 14:20