HOW TO PROGRAM SQLITE
FOR IPHONE iphone programming series Muthu Arumugam iPhone App Consulting
iPhone Programming Guide
1
TABLE OF CONTENTS Introduction
3
Requirements
3
Steps
4
References
11
iPhone Programming Guide
2
Introduction iPhone SDK allows programmers to add database to their application easily using SQLite. This document explains the steps involved in creating a new application to use SQLite feature from scratch.
Requirements • iPhone SDK 2.2 or above • FMDB package from http://gusmueller.com/blog/archives/2008/03/fmdb_for_iphone.html • SQLite Manager - To create and manage database
iPhone Programming Guide
3
Steps Step 1: Create a new project
The windows will look like the following when you name your project as DBSample
iPhone Programming Guide
4
Step 2: Add SQLite library to the project to the Frameworks from <SDK Folder>/usr/lib
iPhone Programming Guide
5
Step 3: Extract and add the FMDB library to the project. Download it from http://www.iphoneappconsulting.com/wp-content/uploads/2009/01/fmdb.zip. Drag and drop the folder to the project.
Step 4: Add a new database to the project under Resources as “base.sqlite”. Use SQLite Manager to create a database and attach it to the project.
iPhone Programming Guide
6
Add a new table called Settings and add 2 columns to that as shown below:
Step 5: Create a new class to have your DB methods. Call it as DB (DB.h and DB.m).
iPhone Programming Guide
7
Step 6: Add some code to insert and select rows from this newly created “Settings” table DB.h
#import #import "FMDB/FMDatabase.h" @interface DB : NSObject { FMDatabase* db; } - (void)addSettings:(NSString *)code with:(NSString *)value; - (void)setSettings:(NSString *)code with:(NSString *)value; - (NSString *)getSettings:(NSString *)code; @end
DB.m #import "DB.h" @implementation DB - init { if(![super init]) return nil; // The database is stored in the application bundle. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:@"base.sqlite"]; db = [FMDatabase databaseWithPath:path]; [db setLogsErrors:TRUE]; [db setTraceExecution:TRUE];
iPhone Programming Guide
8
}
if (![db open]) { NSLog(@"Could not open db."); return 0; } else { NSLog(@"oooooooohooo. DB Open...."); } return self;
- (void)dealloc { [db close]; [super dealloc]; } - (void)addSettings:(NSString *)code with:(NSString *)value { [db executeUpdate:@"insert into settings values (?, ?)", code, value]; } - (void)setSettings:(NSString *)code with:(NSString *)value { [db executeUpdate:@"update settings set value = ? where code = ?", value, code]; } - (NSString *)getSettings:(NSString *)code { NSString *toReturn = [[NSString alloc] init]; FMResultSet *rs = [db executeQuery:@"select value from settings where code = ?", code]; while ([rs next]) { toReturn = [rs stringForColumn:@"value"]; } return toReturn; } @end
iPhone Programming Guide
9
Step 7: Add additional code to the app to call these functions directly.
iPhone Programming Guide
10
References • FMDB for iPhone - http://gusmueller.com/blog/archives/2008/03/fmdb_for_iphone.html • Google code - http://code.google.com/p/flycode/source/browse/trunk/fmdb • SQLite Manager - http://code.google.com/p/sqlite-manager/
iPhone Programming Guide
11