0

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.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Gopinath
  • 5,392
  • 21
  • 64
  • 97
  • @OmarAbdelhafith thanks. Nothing showed in the debugger console except these logs. 2012-06-29 16:32:19.140 SQLiteSampleApp[5697:207] Name : ABCD 2012-06-29 16:32:19.140 SQLiteSampleApp[5697:207] Message : Hello 2012-06-29 16:32:19.141 SQLiteSampleApp[5697:207] Execute Query : insert into creagx values ('ABCD', 'Hello'). Can you please help me? Thanks. – Gopinath Jun 29 '12 at 11:03
  • instead of your executeUpdate, add [db executeQuery:@"select * from creagx"];, does it crash? – Omar Abdelhafith Jun 29 '12 at 11:11
  • @OmarAbdelhafith Thank you. But, database don't have executeQuery property. Onely FMResultsSet having executeQuery. can you help me? thanks. – Gopinath Jun 29 '12 at 11:17
  • lets continue in chat http://chat.stackoverflow.com/rooms/13216/http-stackoverflow-com-questions-11260530-how-to-use-sqlite-in-iphonecomment14 – Omar Abdelhafith Jun 29 '12 at 11:20

2 Answers2

1

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)

ccgus
  • 2,906
  • 23
  • 18
0

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];
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56