views:

197

answers:

1

HELLO i am trying to insert Current date in my database table i have a column dateVisited of type datetime

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"]; //this is the sqlite's format
NSDate *stringTime = [NSDate date];
NSString *formattedDateStringTime = [formatter stringFromDate:stringTime];

sqlStr = [NSString stringWithFormat:@"Insert into AnwaltHistory (id,dateVisited) values (%d,'%@')",Id formattedDateStringTime];

NSLog(@"%@",sqlStr);
BOOL Res = [appDelegate InsUpdateDelData: sqlStr];
if (Res)
{
   NSLog(@"The Following instrunction is excuted successfully !");
}
else
{
   NSLog(@"The Following instrunction Failed to execute !");
}

the statement executes successfully but when i actually go and check the table it says invalid date... i dont know why as i have printed sqlstr i copy paste from the console and paste it in sqlite and run it manually it runs perfectly fine and has the actual date.... can any one guide me where i am mistaken :S....... please

A: 

Why not use:

"Insert into AnwaltHistory (id,dateVisited) values (%d,datetime())",Id
Doug Currie
Thank you very much for your reply.I triedsqlStr = [NSString stringWithFormat:@"Insert into AnwaltHistory (id,dateVisited) values (%d,datetime())",Id];On debugging i figured out it says sqlStr is invalid. and i used NSLog to see whats in sqlStr.Insert into AnwaltHistory (id,dateVisited) values (87403,datetime())which is fine and the statement executed successfully but when i explore the db it shows id but the time is invalid againand then i ran the sqlStr manually it is inserted perfectly with no error.where i am mistaken :SAnd sorry for the late reply
yunas