CREATING AND OPENING A DATABASE
After the necessary library is added to the project, you can open a database for usage. You use the various C functions included with SQLite3 to create or open a database, as demonstrated in the following one-step Try It Out.
TRY IT OUT: Opening a Database
- Using the Databases project created previously, define the openDB method in the DatabasesViewController.m file:
-(void) openDB { //–-create database–- if (sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK ) { sqlite3_close(db); NSAssert(0, @“Database failed to open.”); } } - (void)viewDidLoad { [self openDB]; [super viewDidLoad]; }
How It Works
The sqlite3_open() C function opens a SQLite database whose filename is specified as the first argument:
[self filePath] UTF8String]
In this case, the filename of the database is specified as a C string using the UTF8String method of the NSString class because the sqlite3_open() C function does not understand an NSString object.
The second argument contains a handle to the sqlite3 object, which in this case is db.
If the database is available, it is opened. If the specified database is not found, a new database is created. If the database is successfully opened, the function will return a value of 0 (represented using the SQLITE_0K constant).
The following list from www.sqlite.org/c3ref/c_abort.html shows the result codes returned by the various SQLite functions:
#define SQLITE_0K 0 /* Successful result */ #define SQLITE_ERR0R 1 /* SQL ...
Get Beginning iOS 5 Application Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.