Search code examples
xcode4fmdb

How to insert text field's data into the database using FMDB method


I have a database with 5 columns.. I should update the database with the values typed in the textfields..

-(void)insertData:(int)insGI:(NSString*)insVis:(NSString*)insp4p:(NSString*)insCM:(NSString*)insSC:(NSString*)insCEL
{

[db beginTransaction];

NSString *stringSQL = [NSString stringWithFormat:@"UPDATE CreateGoal SET Column1 = %@, Column2 = %@, Column3 = %@, Column4 = %@, Column5 = %@ WHERE rowid = %d ", insVis, insp4p, insCM, insSC, insCEL, insGI];
[db executeQuery:stringSQL];
[db commit];

}

update a table with new values..

-(IBAction)someaction:(id)sender

{

 sc = text1.text;

NSLog(@"\n \n \n SC = %@",sc);
printf("\n \n \n HHHHHHHHHHH");
p4p = text2.text;
vis = text3.text;
cm = text4.text;
cel = text5.text;
 [[DBManager getInstance]insertData:gid:vis:p4p:cm:sc:cel]; 

}

calling like this.. Problem is the values are not getting stored in the database.. where have i gone wrong?? Please help..

THANKS


Solution

  • you should use executeUpdate for writing on fmdb. Also, I strongly advise you to avoid stringWithFormat and use the parsing capabilities of fmdb, which can perform type checking at runtime.

    [db executeUpdate:@"UPDATE CreateGoal SET Column1 = ?, Column2 = ?", value1, value2, nil]; //check as always the nil at the end
    

    hope it helps!