Beruflich Dokumente
Kultur Dokumente
Ads by Google
Xcode Cocoa
Iphone
Sqlite
Database
From Techotopia
Table of Contents
Next Working with iOS 4 iPhone Databases using Core Data (Xcode 4)
Purchase and download the fully updated iOS 5 Edition of this book in Print ($27.99) or eBook ($12.99) format. iPhone iOS 5 Development Essentials Print and eBook (ePub/PDF) editions contain 56 chapters.
eBookFrenzy.com
In the chapter entitled iOS 4 iPhone Database Implementation using SQLite the basic concepts of integrating a SQLite based database into iOS 4 iPhone-based applications were discussed. In this chapter we will put this knowledge to use by creating a simple example application that demonstrates SQLite based database implementation and management on the iPhone.
Contents
1 About the Example SQLite iPhone Application 2 Creating and Preparing the SQLite Application Project 3 Importing sqlite3.h and declaring the Database Reference 4 Creating the Outlets and Actions 5 Releasing Memory 6 Creating the Database and Table 7 Implementing the Code to Save Data to the SQLite Database 8 Implementing Code to Extract Data from the SQLite Database 9 Designing the User Interface 10 Building and Running the Application 11 Summary
iOS garbage collector for ObjC/C++ reclaims cycles & "in-use" garbage
www.harnixtechnologies.ca/iGC
Ajax RIAs in 100% Java The Most Advanced GWT Framework Live Demos & Free 60-day Trial NuoDB - NewSQL Leader Elastic SQL database for the cloud Download the Beta today Visual SQL to XML Easy to use Data Mapping environment. Try now!
www.ecrion.com
www.SmartClient.com/SmartGWT
www.nuodb.com
techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application_%28Xcode_4%29
1/8
11/18/11
The Link Binary with Libraries section lists the libraries and frameworks already included in the project. To add another library or framework click on the + button to display the full list. From this list, select libsqlite3.dylib and click Add.
techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application_%28Xcode_4%29
2/8
11/18/11
Select the databaseViewController.h file once again and further modify the interface file to add the appropriate outlets and actions:
#import <UIKit/UIKit.h> #import "/usr/include/sqlite3.h" @interface databaseViewController : UIViewController { UITextField *name; UITextField *address; UITextField *phone; UILabel *status; NSString *databasePath; sqlite3 *contactDB; } @property (retain, nonatomic) IBOutlet UITextField *name; @property (retain, nonatomic) IBOutlet UITextField *address; @property (retain, nonatomic) IBOutlet UITextField *phone; @property (retain, nonatomic) IBOutlet UILabel *status; - (IBAction) saveData; - (IBAction) findContact; @end
With the outlets and actions declared in the interface file, edit the databaseViewController.m file and add the @synthesize directives for the outlets and the template methods for the saveData and findContact actions:
#import "databaseViewController.h" @implementation databaseViewController @synthesize name, address, phone, status; - (void) saveData { } -(void) findContact { } . . . @end
Releasing Memory
Having allocated memory in implementing the above outlets, it is important that we add code to free up any resources that were allocated during execution of the application. To do so, edit the coreDataViewController.m file again and modify the viewDidUnload and dealloc methods as follows:
- (void)viewDidUnload { self.name = nil; self.address = nil; self.phone = nil; self.status = nil; } - (void)dealloc { [name release]; [address release]; [phone release]; [status release]; [super dealloc]; }
techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application_%28Xcode_4%29
3/8
11/18/11
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) { char *errMsg; const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) { status.text = @"Failed to create table"; } sqlite3_close(contactDB); } else { status.text = @"Failed to open/create database"; } } [filemgr release]; [super viewDidLoad]; }
The code in the above method performs the following tasks: Identifies the applications Documents directory and constructs a path to the contacts.db database file. Creates an NSFileManager instance and subsequently uses it to detect if the database file already exists. If the file does not yet exist the code converts the path to a UTF-8 string and creates the database via a call to the SQLite sqlite3_open() function, passing through a reference to the contactDB variable declared previously in the interface file. Prepares a SQL statement to create the contacts table in the database. Reports the success or failure of the operation via the status label. Closes the database and performs a memory clean up.
The next step in our application development process is to implement the action for the find button.
techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application_%28Xcode_4%29
4/8
11/18/11
sqlite3_stmt
This code opens the database and constructs a SQL SELECT statement to extract any records in the database that match the name entered by the user into the name text field. The SQL statement is then executed. A return value of SQLITE_ROW indicates that at least one match has been located. In this case the first matching result data is extracted, assigned to NSString objects and displayed in the appropriate text fields. As an alternative, a while loop could have been constructed to display all matching results. For the purposes of keeping this example simple, however, we will display only the first match. The code then updates the status label to indicate whether a match was found, releases memory where necessary and closes the database.
techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application_%28Xcode_4%29
5/8
11/18/11
Before proceeding, stretch the status label (located above the two buttons) so that it covers most of the width of the view as shown above. Finally, edit the label and remove the word Label so that it is blank. Next, connect the three text fields and status label to the name, address, phone and status outlets respectively by holding down the Ctrl key and clicking and dragging from Files Owner object to the component in the view. From the resulting menu select the outlet corresponding to the selected view object. The last step involves connecting the two buttons to the corresponding actions. First, display the Connections Inspector (View -> Utilities -> Connections Inspector) then select the Save button in the editing canvas. Click inside the small circle next to the Touch Up Inside event in the Connections Inspector window and drag the blue line to the Files Owner. To establish the connection, select saveData from the resulting menu. Repeat these steps to connect the Find button to the findContact action method.
eBookFrenzy.com
techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application_%28Xcode_4%29
6/8
11/18/11
Summary
In this chapter we have looked at the basics of storing data on an iPhone using the SQLite database environment. For developers unfamiliar with SQL and reluctant to learn it, an alternative method for storing data in a database involves the use of the Core Data framework. This topic will be covered in detail in the next chapter entitled Working with iOS 4 iPhone Databases using Core Data.
Purchase and download the fully updated iOS 5 Edition of this book in Print ($27.99) or eBook ($12.99) format. iPhone iOS 5 Development Essentials Print and eBook (ePub/PDF) editions contain 56 chapters.
eBookFrenzy.com
Previous Table of Contents Next iOS 4 iPhone Database Working with iOS 4 iPhone Implementation using Databases using Core Data SQLite (Xcode 4) (Xcode 4) Retrieved from "http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application_%28Xcode_4%29"
techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application_%28Xcode_4%29
7/8
11/18/11
This page was last modified 18:09, 31 March 2011. Copyright 2011 Techotopia.com. All Rights Reserved.
techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application_%28Xcode_4%29
8/8