Can you please tell me how to create SQLite DB in iPhone and to perform CRUD operations from objective c program to 开发者_StackOverflowdatabases. 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.
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.
#import in your source file.
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); }
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);
Close the database connection with sqlite3_close (database);
For more info on the C interface, see http://www.sqlite.org/cintro.html.
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.
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.
精彩评论