3. Let s import sqlite3.h and declaring the Database Reference

Similar documents
โปรแกรมบ นท ก ช อ และ อ เมล โดยจ ดเก บข อม ลลงไปท SQLite

Send from your App Part 1

Xcode Application note

2. Create the User Interface: Open ViewController.xib or MainStoryBoard.storyboard by double clicking it.

ios App Development for Everyone

Data storage and retrieval in ios

INTRODUCTION TO IOS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 13 02/22/2011

2. About iphone ios 5 Development Essentials. 5. Joining the Apple ios Developer Program

Praktikum Entwicklung von Mediensystemen mit

ios Development Tutorial Nikhil Yadav CSE 40816/60816: Pervasive Health 09/09/2011

Creating a Custom Class in Xcode

Learn iphone and ipad game apps development using ios 6 SDK. Beginning. ios 6 Games. Development. Lucas Jordan. ClayWare Games tm

Assignment I Walkthrough

Praktikum Entwicklung von Mediensystemen mit ios

An Introduction to Modern Software Development Tools Creating A Simple GUI-Based Tool Appleʼs XCode Version 3.2.6

Visual Basic 2010 Essentials

Assignment 2: Matchismo 2

Enterprise Historian 3BUF D1 Version 3.2/1 Hot Fix 1 for Patch 4 Release Notes

ios App for Mobile Website! Documentation!

TakeMySelfie ios App Documentation

Start Developing ios Apps Today

ios App Development for Everyone

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Macros in Word & Excel

M, N, O F, G, H. network request, 101 ParseFacebookUtilities SDK, 100 profile, 100 user_about_me, 101 -(void)updateindicator, 101

Tutorial: Hybrid App Package Development. Sybase Unwired Platform 2.2 SP02

Merging Labels, Letters, and Envelopes Word 2013

MICROSOFT OUTLOOK 2010 WORK WITH CONTACTS

Introduction to Word 2007

1.5 MONITOR. Schools Accountancy Team INTRODUCTION

Tag Specification Document

Avery Wizard: Using the wizard with Microsoft Word. This is a simple step-by-step guide showing how to use the Avery wizard in word

How To Create A Hyperlink In Publisher On Pc Or Macbookpress.Com (Windows) On Pc/Apple) On A Pc Or Apple Powerbook (Windows 7) On Macbook Pressbook (Apple) Or Macintosh (Windows 8

Appendix A How to create a data-sharing lab

Adobe Captivate Tips for Success

Mastering Mail Merge. 2 Parts to a Mail Merge. Mail Merge Mailings Ribbon. Mailings Create Envelopes or Labels

Working with sections in Word

USB Security Key Instructions

Mobile App Design and Development

SMART CONSOLE Software and Firmware Upgrade Procedure

iw Document Manager Cabinet Converter User s Guide

Introduction to dobe Acrobat XI Pro

HOW TO BURN A CD/DVD IN WINDOWS XP. Data Projects

Create a new file/canvas to work with by going to the file menu and selecting new.

BASIC IPHONE PROGRAMMING Case: Dictionary Application

Advantage Cloud Access: Microsoft Remote Desktop for Android

College of Continuing Education Video Production Room

IT Quick Reference Guides Using Windows 7

edgebooks Quick Start Guide 4

Lab - Building an Internet of Things Application Hands-On Lab

1 от :45

Xilinx ISE. <Release Version: 10.1i> Tutorial. Department of Electrical and Computer Engineering State University of New York New Paltz

MA-WA1920: Enterprise iphone and ipad Programming

Microsoft PowerPoint Exercises 4

Star Micronics Cloud Services ios SDK User's Manual

Shipment Label Header Guide

Introduction: The Xcode templates are not available in Cordova or above, so we'll use the previous version, for this recipe.

Your First App Store Submission

Training Manual. Version 6

Windows 8.1 Update 1 Supplement

TUTORIAL 4 Building a Navigation Bar with Fireworks

Transitioning from TurningPoint 5 to TurningPoint Cloud - LMS 1

LabVIEW Day 6: Saving Files and Making Sub vis

BID2WIN Workshop. Advanced Report Writing

GRAPHICAL WORKFLOW DESIGN USING WORKFLOW MANAGER - BASICS

iphone ios 6 Development Essentials

Lesson 5 Build Transformations

Saving GroupWise as Text Files

RAID installation guide for Silicon Image SiI3114

6. If you want to enter specific formats, click the Format Tab to auto format the information that is entered into the field.

How to test and debug an ASP.NET application

Optional Lab: Install, Configure, and Partition a Second Hard Drive in Windows Vista

Mobile: Getting Started with Workday for ipad

Configuring the SST DeviceNet OPC Server

Ohio University Computer Services Center August, 2002 Crystal Reports Introduction Quick Reference Guide

Results CRM 2012 User Manual

Intelli-M Access Quick Start Guide

Office of History. Using Code ZH Document Management System

Setting up Sharp MX-Color Imagers for Inbound Fax Routing to or Network Folder

First Bytes Programming Lab 2

3. Locate the different selections of Styles from the Home Tab, Styles Group

Maximizing the Use of Slide Masters to Make Global Changes in PowerPoint

Word 2010: Mail Merge to with Attachments

Installing Client Profiles 6.0

Microsoft Outlook Introduction

Download and Install the Citrix Receiver for Mac/Linux

5. Tutorial. Starting FlashCut CNC

Microsoft Word 2010 Tutorial

Using Microsoft Office XP Advanced Word Handout INFORMATION TECHNOLOGY SERVICES California State University, Los Angeles Version 1.

Writer Guide. Chapter 15 Using Forms in Writer

JAVS Scheduled Publishing. Installation/Configuration... 4 Manual Operation... 6 Automating Scheduled Publishing... 7 Windows XP... 7 Windows 7...

Logbook Entry Creator Program

Witango Application Server 6. Installation Guide for Windows

Appendix K Introduction to Microsoft Visual C++ 6.0

Voyager Reporting System (VRS) Installation Guide. Revised 5/09/06

iphone Objective-C Exercises

Your First ios Application

Faculty Web Site with WCM

Horizon Debt Collect. User s and Administrator s Guide

SQL Server 2005: Report Builder

Transcription:

An ios SQLite Database Example Application We are going to design an application to store an address book, that is contact information (names, addresses and telephone numbers) in a SQLite database. In addition to data storage, a feature will also be implemented to allow the user to search the database for the address and phone number of a specified contact name. Some knowledge of SQL and SQLite database concepts is assumed. 1. Create a new Xcode Single View Application project. Call it something like Database or another title of your choice. Use the Storyboard support and enable Automatic Reference Counting (ARC). 2. Once the project has been created, the next step is to configure the project to include the SQLite dynamic library (libsqlite3.dylib) during the link phase of the build process. Failure to include this library will result in build errors. To add this library, select the target entry in the Xcode project navigator (the top entry with the product name) to display the summary information panel. Select the Build Phases tab to display the build information. 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. Search and select libsqlite3.dylib. Click Add. 3. Let s import sqlite3.h and declaring the Database Reference Before we can create a database we need to declare a variable pointer to a structure of type sqlite3 that will act as the reference to our database. Since we will be working with the database in the view controller for our application the best place to declare this variable is in the ViewController.h file. Since we also need to import the sqlite3.h header file into any files where we make use of SQLite this is also an ideal place to include the file. Now is also a good time to declare an NSString property in which to store the path to the database. Within the main Xcode project navigator, select the ViewController.h file and modify it as follows: Add to the top of ViewController.h:

#import <sqlite3.h> Add the following two properties to this same file: @property (strong, nonatomic) NSString *databasepath; @property (nonatomic) sqlite3 *contactdb; The finished file now looks like this: #import <UIKit/UIKit.h> #import <sqlite3.h> @interface ViewController : UIViewController @property (strong, nonatomic) NSString *databasepath; @property (nonatomic) sqlite3 *contactdb; @end 4. Now you need to design the user interface. Select the MainStoryboard.storyboard file to edit the user interface and drag and drop components from the Object library (View -> Utilities -> Object Library) onto the view canvas and edit properties so that the layout appears as shown below:

The user interface for an iphone SQLite Database example Note: Before proceeding, stretch the status label (located above the two buttons) so that it covers most of the width of the view. Finally, edit the label and remove the word Label so that it is blank. 5. Next you will wire the components to ViewController.h. Select the top most text field object in the view canvas, display the Assistant Editor panel (tuxedo icon) and verify that the editor is displaying the contents of the ViewController.h file. Ctrl-click on the text field object and drag it to a position just below the @interface line. Release the line and in the resulting

connection dialog establish an outlet connection named thename. Repeat the above steps to establish outlet connections for the remaining text fields and the label object to properties named theaddress, thephone and thestatus. Ctrl-click on the Save button object and drag the line to the area immediately beneath the newly created outlet in the Assistant Editor panel. Release the line and, within the resulting connection dialog, establish an Action method on the Touch Up Inside or Touch Down event configured to call a method named savedata. Repeat this step to create an action connection from the Find button to a method named findcontact. On completion of these steps, the ViewController.h file should read as follows: @property (weak, nonatomic) IBOutlet UITextField *thename; @property (weak, nonatomic) IBOutlet UITextField *theaddress; @property (weak, nonatomic) IBOutlet UITextField *thephone; @property (weak, nonatomic) IBOutlet UILabel *thestatus; - (IBAction)saveData:(UIButton *)sender; - (IBAction)findContact:(UIButton *)sender; DON T synthesize the properties inside of ViewController.m. Instead we are going to take advantage of an auto-synthesize feature of the latest ios SDK. We get auto synthesize if we use the names above plus the underscore _ in front of the name. For example: thename is synthesized as _thename theaddress is synthesized as _theaddress thephone is synthesized as _thephone thestatus is synthesized as _thestatus We also get _databasepath and _contractdb automatically as well. 6. Creating the Database and Table When the application is launched it will need to check whether the database file already exists and, if not, create both the database file and a table within the database in which to store the contact

information entered by the user. The code to perform this task can be placed in the viewdidload method of our view controller class. Select the ViewController.m file, scroll down to the viewdidload method and modify it as follows: - (void)viewdidload [super viewdidload]; NSString *docsdir; NSArray *dirpaths; // Get the documents directory dirpaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); docsdir = dirpaths[0]; // Build the path to the database file _databasepath = [[NSString alloc] initwithstring: [docsdir stringbyappendingpathcomponent:@"contacts.db"]]; NSFileManager *filemgr = [NSFileManager defaultmanager]; if ([filemgr fileexistsatpath: _databasepath ] == NO) const char *dbpath = [_databasepath UTF8String]; 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, PHONE TEXT)"; if (sqlite3_exec(_contactdb, sql_stmt, NULL, NULL, &errmsg)!= SQLITE_OK)

_thestatus.text = @"Failed to create table"; sqlite3_close(_contactdb); else _thestatus.text = @"Failed to open/create database"; The code in the above method performs the following tasks: Identifies the application s 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 otherwise of the operation via the status label. Closes the database. 7. Implementing the Code to Save Data to the SQLite Database The saving of contact data to the database is the responsibility of the savedata action method. This method will need to open the database file, extract the text from the three text fields and construct and execute a SQL INSERT statement to add this data as a record to the database. Having done this, the method will then need to close the database. In addition, the code will need to clear the text fields ready for the next contact to be entered, and update the status label to reflect the success or failure of the operation. In order to implement this behavior, we need to modify the savedata method as follows: - (IBAction)saveData:(UIButton *)sender sqlite3_stmt *statement;

const char *dbpath = [_databasepath UTF8String]; if (sqlite3_open(dbpath, &_contactdb) == SQLITE_OK) NSString *insertsql = [NSString stringwithformat: @"INSERT INTO CONTACTS (name, address, phone) VALUES (\"%@\", \"%@\", \"%@\")", self.thename.text, self.theaddress.text, self.thephone.text]; const char *insert_stmt = [insertsql UTF8String]; sqlite3_prepare_v2(_contactdb, insert_stmt, -1, &statement, NULL); if (sqlite3_step(statement) == SQLITE_DONE) self.thestatus.text = @"Contact added"; self.thename.text = @""; self.theaddress.text = @""; self.thephone.text = @""; else self.thestatus.text = @"Failed to add contact"; sqlite3_finalize(statement); sqlite3_close(_contactdb); 8. Implementing the Code to Find Data in the SQLite Database The next step is to implement the action for the findcontact button. Implementing Code to Extract Data from the SQLite Database As previously indicated, the user will be able to extract the address and phone number for a contact by entering the name and touching the find button. To this end, the Touch Up Inside event of the find button has been connected to the findcontact method, the code for which is outlined below: - (IBAction)findContact:(UIButton *)sender const char *dbpath = [_databasepath UTF8String];

sqlite3_stmt *statement; if (sqlite3_open(dbpath, &_contactdb) == SQLITE_OK) NSString *querysql = [NSString stringwithformat: @"SELECT address, phone FROM contacts WHERE name=\"%@\"",_thename.text]; const char *query_stmt = [querysql UTF8String]; if (sqlite3_prepare_v2(_contactdb, query_stmt, -1, &statement, NULL) == SQLITE_OK) if (sqlite3_step(statement) == SQLITE_ROW) NSString *addressfield = [[NSString alloc] initwithutf8string:(const char *) sqlite3_column_text(statement, 0)]; _theaddress.text = addressfield; NSString *phonefield = [[NSString alloc] initwithutf8string:(const char *) sqlite3_column_text(statement, 1)]; _thephone.text = phonefield; _thestatus.text = @"Match found"; else _thestatus.text = @"Match not found"; _theaddress.text = @""; _thephone.text = @""; sqlite3_finalize(statement); sqlite3_close(_contactdb); Here is what the above source code is doing: 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 and 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 and closes the database. 9. Optional: Make the keyboard go away when user finishes entering text. Otherwise, the keyboard might cover up the buttons to save and search. Wire each of the textfield, Did End on Exit events to your ViewController.h file as follows: - (IBAction)NameDone:(UITextField *)sender; - (IBAction)addressDone:(UITextField *)sender; - (IBAction)phoneDone:(UITextField *)sender; Add the following code to resign the first responded in your ViewController.m implementation of these methods: - (IBAction)NameDone:(UITextField *)sender [_thename resignfirstresponder]; - (IBAction)addressDone:(UITextField *)sender [_theaddress resignfirstresponder]; - (IBAction)phoneDone:(UITextField *)sender [_thephone resignfirstresponder]; 10. Building and Running the Application

Enter details for a few contacts, pressing the Save button after each entry. Be sure to check the status label to ensure the data is being saved successfully. Finally, enter the name of one your contacts and click on the Find button. Assuming the name matches a previously entered record, the address and phone number for that contact should be displayed and the status label updated with the message Match found. Now, go use data in your other applications!