Reputation: 5442
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.
Upvotes: 0
Views: 1530
Reputation: 2916
there's documentation on the correct way to use FMDB, and using stringWithFormat: isn't it: https://github.com/ccgus/fmdb (search for "SHOULD NOT do this").
Instead you want to do something like this:
database = [FMDatabase databaseWithPath:path];
[database open]; // FIXME check the return value
[database executeUpdate:@"create table test(name text, message text)"]; // FIXME check for an error
// since we're only performing a single update, there's no need for a transaction
[executeUpdate:@"INSERT INTO test (name, message) VALUES (?, ?)", @"Yuva", @"Hi there"];
-gus (the guy who wrote FMDB)
Upvotes: 1
Reputation: 21221
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];
Upvotes: 0