yKim
yKim

Reputation: 63

My sqlite query(update) looks not working in iOS

I want to update one of the record in sqlite table like this at once.

travellName : self.savedName -> self.editedName

currency : self.savedCurrency -> self.editedCurrency

Rate : self.savedRate -> self.editedRate

startBudget : self.savedBudget -> self.editedBudget

leftAssets : self.savedLeftAssets -> self.editedLeftAssets

enter code here

NSString *qsql = [NSString stringWithFormat:@"UPDATE Travell SET travellName = 
'%@' and currency = '%@' and changeRate = '%@' and startBudget = '%@' and 
leftBudget = '%@' WHERE travellName = '%@' and currency = '%@' and changeRate =
'%@' and startBudget ='%@' and leftBudget = '%@'", self.editedName,   
self.editedCurrency, self.editedRate,self.editedBudget, self.editedLeftAssets, 
self.savedName, self.savedCurrency, self.savedRate, self.savedBudget, 
self.savedLeftAssets];

if(sqlite3_exec(db, [qsql UTF8String], NULL, NULL, &err) != SQLITE_OK){
        sqlite3_close(db);
        NSLog(@"Failed to update leftBudget");
}
else{
        NSLog(@"Success to update leftBudget");
}

It is my code. I have lots of field to update at once. is there any fault at query??

I've checked all of the value of variable and there were no problem. Weird, sqlite3_exec(...) returns SQLITE_OK and NSLog(@"Success to update leftBudget") was executed. but the problem is updating table wasn't executed properly.. I have no idea as program says SQLITE_OK but table wasn't update properly..

Upvotes: 0

Views: 80

Answers (1)

CL.
CL.

Reputation: 180060

In the UPDATE statement, multiple columns to be set must be separated not with AND but with commas.

SET travellName = '...' and currency = '...' and ... is interpreted as setting the single column travellName to the value of the expression '...' and currency = '...' and ..., in which AND is a boolean operator; the final value of this expression is either 0 or 1.

(And this will blow up when the travel name contains a quote. You should use sqlite3_mprintf to format the strings correctly.)

Upvotes: 1

Related Questions