tags:

views:

53

answers:

3

Hello ,

Can you please tell me how to create SQLite DB in iPhone and to perform CRUD operations from objective c program to databases. How to do it programmatically??

I know to create DB and table through command line, but how to do it programmatically??

Help me..

Thank you.

A: 

It might be easier to user CoreData. Start googling about to use that or look at some sample projects. Will take probably 2-3 days to learn Core Data but it does CRUD a lot better.

John Ballinger
A: 

By far the best example (if you can find it) is Apple's sample code called SQLiteBooks. It came with versions of XCode prior to Core data. I have a copy lying around, so if you can't find it send me a message and we can arrange something.

That will give you a very good start.

John Smith
+1  A: 
  1. Add the libsqlite3.dyllib library to your project. Right-click on the Frameworks group, select Add->Existing Frameworks... and scroll down to select and add libsqlite3.dyllib.

  2. #import in your source file.

  3. Open or create a database file with the path in an NSString file using this code:

    int error = sqlite3_open ([file cStringUsingEncoding:NSUTF8StringEncoding], &database);
    if (error != SQLITE_OK) {
        NSLog (@"Error result from sqlite3_open(): %d", error);
        sqlite3_close (database);
    }
    
  4. Execute commands in an NSString aQuery with this code:

    char *errorMessage = nil;
    int error = sqlite3_exec (database, [aQuery cStringUsingEncoding:NSUTF8StringEncoding], nil, nil, &errorMessage);
    if (error != SQLITE_OK)
        NSLog (@"Error result from sqlite3_exec(): %d: %s", error, errorMessage);
    if (errorMessage != nil)
        sqlite3_free(errorMessage);
    
  5. Close the database connection with sqlite3_close (database);

For more info on the C interface, see http://www.sqlite.org/cintro.html.

lucius