0

I am trying to insert some values in my database in AppDelegate in Application didFinishLaunchingWithOptions however each time it fails to insert data.

code

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *path = [docsPath stringByAppendingPathComponent:@"db.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:path];
[database open];
FMResultSet *results = [database executeQuery:@"select * from settings"];
if(results == nil)
{
 NSLog(@"Creating DB");
 [database beginTransaction];
 [database executeUpdate:@"create table settings(id int primary key, defaultaction text, beepsound text, vibrateeffect text, level text)"];
 NSString *query = [NSString stringWithFormat:@"insert into settings(id,defaultaction,beepsound,vibrateeffect,level) values (%d, '%@', '%@','%@','%@',)",11,@"auto", @"YES", @"YES", @"HIGH"];
 NSLog(@" %@",path);
 BOOL y= [database executeUpdate:query];
 if (!y)
 {
 NSLog(@"insert failed!!");
 }

 [database commit];
 [database close];


 }
halfer
  • 19,824
  • 17
  • 99
  • 186
Saty
  • 2,563
  • 3
  • 37
  • 88
  • 1
    Doesn't that API provide you with any error messages? If not then you should stop using it, as it's crap. If it does, then retrieve them and report them. – trojanfoe Feb 21 '13 at 11:13
  • 1
    @trojanfoe...I have been using this from very long time and it was working pretty good. Today morning I found this type of issue. I am not able to understand what to do... – Saty Feb 21 '13 at 11:17
  • Like I said, retrieve the error message and it will give you a clue. Without this error message no one can help you anyway. – trojanfoe Feb 21 '13 at 11:17

2 Answers2

2

i think the problem is in your code of insert query

NSString *query = [NSString stringWithFormat:@"insert into settings(id,defaultaction,beepsound,vibrateeffect,level) values (%d, '%@', '%@','%@','%@',)",11,@"auto", @"YES", @"YES", @"HIGH"];

You have added one more , at last in values() check and remove last , it will definitely work

NSString *query = [NSString stringWithFormat:@"insert into settings(id,defaultaction,beepsound,vibrateeffect,level) values (%d, '%@', '%@','%@','%@')",11,@"auto", @"YES", @"YES", @"HIGH"];

try this query

Naimish Karia
  • 346
  • 1
  • 6
1
NSString *query = @"INSERT into settings(id,defaultaction,beepsound,vibrateeffect,level) 
VALUES (%@, %@, %@,%@,%@)";

BOOL y = [database executeUpdate:query, [NSNumber numberWithInt:11],@"auto", @"YES", @"YES", @"HIGH"];

Try changing your query to the above stated query, it will work for sure if the table was created successfully.

Good luck.

Ravi Raman
  • 1,070
  • 9
  • 16