Search code examples
iphoneiossqlitefmdb

How to use SQLite in iPhone?


Am new to SQLite using in iPhone. I have used google and got some information regarding SQLite for iPhone. I have downloaded the sample source code from here. I have added the code to create table like below,

database = [FMDatabase databaseWithPath:path];
[database open];
[database executeUpdate:@"create table test(name text, message text)"];

And i have used below code to insert values in the table:

    NSString *nameStr = nameText.text;
    NSString *messageStr = ageText.text;

    NSString *executeQuery = [NSString stringWithFormat:@"insert into test values ('%@', '%@')",nameStr, messageStr];
    NSLog(@"Execute Query : %@", executeQuery);
    //[database executeUpdate:executeQuery];

    [database executeUpdate:@"insert into test(name, message) values(?,?)", nameStr,messageStr,nil];

But, the app getting crash in the line [database executeUpdate:@"insert into test(name, message) values(?,?)", nameStr,messageStr,nil]; I can't get the exact reason for this crash.

If we are using FMDatabase in iPhone is secure and it is preferable for iPhone apps? Can anyone please help on these issues? Thanks in advance.


Solution

  • Use this code

    database = [FMDatabase databaseWithPath:path];
    [database open];
    [database executeUpdate:@"create table test(name text, message text)"];
    [database beginTransaction];
    
    NSString *executeQuery = [NSString stringWithFormat:@"INSERT INTO test (name, message) VALUES (\"%@\", \"%@\")",@"Yuva", @"Hi there",nil];
    NSLog(@"Execute Query : %@", executeQuery);
    [database executeUpdate:executeQuery];
    [database commit];