I'm not able to do an update, but inserts and selects work fine.
Note:
1) I'm using the wrapper from: https://github.com/misato/SQLiteManager4iOS
2) The same code works for the INSERT statement, but not for the update
//NSString* sqlStr = (@"INSERT INTO sbu (sbuName) VALUES ( 'rrrr' );)"); //WORKS
3) Here is the code:
The code associated with this update as follows:
my inline code
NSString* sqlStr = (@"UPDATE User SET Name = 'wweerr' WHERE Id = 19"); //using a direct sql to verify if it works - does not
SQLiteManager* dbManager =[[SQLiteManager alloc]initWithDatabaseNamed:@"data.db"];
NSError* error = [dbManager doQuery:sqlStr];
from the library:
- (NSError *)doQuery:(NSString *)sql {
NSError *openError = nil;
NSError *errorQuery = nil;
//Check if database is open and ready.
if (db == nil) {
openError = [self openDatabase];
}
if (openError == nil) {
sqlite3_stmt *statement;
const char *query = [sql UTF8String];
sqlite3_prepare_v2(db, query, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_ERROR) {
const char *errorMsg = sqlite3_errmsg(db);
errorQuery = [self createDBErrorWithDescription:[NSString stringWithCString:errorMsg encoding:NSUTF8StringEncoding]
andCode:kDBErrorQuery];
}
//NSLog(@"sql error: %@", error)
NSInteger result = sqlite3_finalize(statement);
errorQuery = [self closeDatabase];
}
else {
errorQuery = openError;
}
I found out the pitfall. I have wrapped the string using the method: initWithFormat.
I just assign a variable like this:
And now I just pass that variable in:
TODO/TO_ASK:
Why this same thing does NOT work with:
I would try with "WHERE Id = '19'" (19 with single quotes) because the field may have been defined as text since sqlite fields are weakly typed.