iOSAppDev
iOSAppDev

Reputation: 2783

FMDatabase: perform the operation: Insert, Update, Select, Delete

I am developing iPhone application using SQLite. I decided to use fmdb. Using fmdb, how do I perform the following operation:-Insert, Update, Select, Delete?

As I can't develop application using FMDatabase. I downloaded FMDatabase files which contain following files namely =>

  1. FMDatabase.h
  2. FMDatabase.m
  3. FMResultSet.h
  4. FMResultSet.m
  5. FMDatabaseAdditions.h
  6. FMDatabaseAdditions.m
  7. fmdb.m

But the file fmdb.m also contains the main function. So it is conflicting with my applications MAIN function.

Upvotes: 1

Views: 3204

Answers (2)

Cesar A. Rivas
Cesar A. Rivas

Reputation: 1355

to use fmdb in your code, just add the files you list to your project, except the main.m file. Here is an example of using it, when the app finish launching, a SQLite database will be created.

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application 


    FMDatabase* db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
    if (![db open]) {
        NSLog(@"Could not open db.");

    }

    [db executeUpdate:@"create table test (a text, b text, c integer, d double, e double)"];

    NSLog(@"%@",[db databasePath]);
    [db beginTransaction];
    int i = 0;
    while (i++ < 20) {
        [db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, ?)" ,
         @"hi'", // look!  I put in a ', and I'm not escaping it!
         [NSString stringWithFormat:@"number %d", i],
         [NSNumber numberWithInt:i],
         [NSDate date],
         [NSNumber numberWithFloat:2.2f]];
    }
    [db commit];

    [db close];


}

Notice, I'm just pasting some code from fmdb main.m file, into applicationDidFinishLaunching.

Upvotes: 3

Ole Begemann
Ole Begemann

Reputation: 135548

fmdb.m is a file that contains FMDB sample code. You should not include this file in your Xcode project. But you should review it to see how to work with FMDB. It contains a lot of well-commented examples.

Upvotes: 3

Related Questions