Reputation: 479
I have done lot of googling on updating the created sqlite table still i did not able to update my table in my sample app .Can any one please tell me what is wrong with my below code .IT works fine till sqlite3_prepare_v2.Once it reach if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL)==SQLITE_OK) condition it is not going into this if() condition can any one tell what is happening here?
const char *dbpath = [[self DBPath] UTF8String];
if(sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySql=[NSString stringWithFormat:@"UPDATE Table1 SET AppEndTime = %@ WHERE AppID= %d",AppEndTime,appID];
const char *sql=[querySql UTF8String];
if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL)==SQLITE_OK){
sqlite3_bind_int(statement, 1, sessionID);
sqlite3_bind_text(statement, 6, [sessionEndTime UTF8String], -1, SQLITE_TRANSIENT);
}
}
char* errmsg;
sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);
if(SQLITE_DONE != sqlite3_step(statement)){
NSLog(@"Error while updating. %s", sqlite3_errmsg(database));
}
else{
sqlite3_reset(statement);
}
sqlite3_finalize(statement);
sqlite3_close(database);
Upvotes: 0
Views: 123
Reputation: 91
Please replace you code as given below.I hope it will solve your problem.
if (sqlite3_prepare_v2(database, sql, -1, &Statement1, NULL) == SQLITE_OK) {
sqlite3_bind_text(Statement1, 1, [status UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(Statement1, 2, [messageID UTF8String], -1, NULL);
int success = sqlite3_step(Statement1);
if(success != SQLITE_ERROR)
{
// NSLog(@"Success");
}
sqlite3_finalize(Statement1);
}
Upvotes: 1
Reputation: 20021
Please make sure your Sqlite file is in the documents directory,Not in project folder. Update and insert will work if and only if the file is in the document directory
Upvotes: 0
Reputation: 1689
Here is my code which update created table. Have a look hope it'll help you.
-(BOOL)updateMyTable{
BOOL isGood = YES;
@try {
NSString *fName = [user_dic valueForKey:@"fName"];
NSString *lName = [user_dic valueForKey:@"lName"];
NSString *email_id = [user_dic valueForKey:@"email_id"];
NSString *employee_id = [user_dic valueForKey:@"employee_id"];
fName= [fName stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
lName= [lName stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
email_id= [email_id stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
employee_id= [employee_id stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
// NSString *autoId = [user_dic valueForKey:@"autoId"];
NSString *sql = [NSString stringWithFormat:@"UPDATE tbl_profile SET fName = '%@', lName = '%@', email_id = '%@' ,employee_id = '%@' WHERE autoId = '%@'",fName,lName,email_id,employee_id,autoId];
[database executeUpdate:sql,nil];
if (MyDelegate.isLogging) {
NSLog(@"edit user QUERY---- >>>>%@",sql);
NSLog(@"edit user RESULT CODE ---- >>>>%d:", [database lastErrorCode]);
NSLog(@"edit user RESULT ERROR MESSAGE ---- >>>>%@",[database lastErrorMessage]);
}
}@catch (NSException *exception) {
isGood = NO;
}
@finally {
}
return isGood;
Upvotes: 0