LINKING TO THE SQLITE3 LIBRARY

To use a SQLite3 database in your application, you first need to add the libsqlite3.dylib library to your Xcode project. The following Try It Out demonstrates how. You will need to download the code files indicated for this exercise and the rest of the Try It Out features in this chapter.

TRY IT OUT: Preparing Your Project to Use SQLite3

image

  1. Using Xcode, create a new Single View Application (iPhone) project and name it Databases. Use the project name as the Class Prefix. Ensure that you have the Use Automatic Reference Counting option unchecked.
  2. Select the project name and then click the Build Phases tab on the right (see Figure 11-1). Click the “+” button shown in the Link Binary with Libraries section to add the libsqlite3.dylib library to it. After this, the library will be added to the project (see Figure 11-2).

    image

    FIGURE 11-1

    image

    FIGURE 11-2

  3. In the DatabasesViewController.h file, declare a variable of type sqlite3, as well as a few methods (see the code in bold). You will define the various methods throughout this chapter.
    #import <UIKit/UIKit.h>
    #import “sqlite3.h”
    
    @interface DatabasesViewController : UIViewController
    {
        sqlite3 *db;
    }
    
    -(NSString *) filePath;
    -(void) openDB;
    -(void) createTableNamed:(NSString *) tableName
                  withField1:(NSString *) field1
                  withField2:(NSString *) field2;
    -(void) insertRecordIntoTableNamed:(NSString *) tableName
                            withField1:(NSString *) field1
                           field1Value:(NSString *) field1Value
                             andField2:(NSString *) field2
                           field2Value:(NSString *) field2Value;
    -(void) getAllRowsFromTableNamed: (NSString *) tableName;
    
    @end
  4. In the DatabasesViewController.m file, define the filePath method as shown in bold:
    #import “DatabasesViewController.h”
    
    @implementation DatabasesViewController
    
    -(NSString *) filePath {
         NSArray *paths =
             NSSearchPathForDirectoriesInDomains(
              NSDocumentDirectory, NSUserDomainMask, YES);
    
        NSString *documentsDir = [paths objectAtIndex:0];
        return [documentsDir stringByAppendingPathComponent:@“database.sql”];
    }

How It Works

In order to work with SQLite3, you must link your application to a dynamic library called libsqlite3.dylib. The libsqlite3.dylib that you selected is an alias to the latest version of the SQLite3 library. On an actual iPhone device, the libsqlite3.dylib is located in the /usr/lib/ directory.

To use a SQLite database, you need to create an object of type sqlite3:

sqlite3 *db;

The filePath method returns the full path to the SQLite database that will be created in the Documents directory on your iPhone (within your application's sandbox):

-(NSString *) filePath {
    NSArray *paths =
        NSSearchPathForDirectoriesInDomains(
    NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@“database.sql”];

image NOTE Chapter 10 discusses the various folders that you can access within your application's sandbox.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.7.117