0

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.

asked Jul 28, 2010 at 5:06

3 Answers 3

1
  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.

answered Jul 28, 2010 at 5:28
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Jul 28, 2010 at 5:28

Comments

0

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.

answered Jul 28, 2010 at 5:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.