Iphone Programming With SQLite
Iphone Programming With SQLite
FOR IPHONE
iphone programming series
Muthu Arumugam
iPhone App Consulting
Introduction 3
Requirements 3
Steps 4
References 11
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 fea-
ture from scratch.
Requirements
The windows will look like the following when you name your project as DBSample
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.
Step 5: Create a new class to have your DB methods. Call it as DB (DB.h and DB.m).
DB.h
#import <Foundation/Foundation.h>
#import "FMDB/FMDatabase.h"
@interface DB : NSObject {
FMDatabase* db;
}
@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, NSUserDomain-
Mask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory
stringByAppendingPathComponent:@"base.sqlite"];
db = [FMDatabase databaseWithPath:path];
[db setLogsErrors:TRUE];
[db setTraceExecution:TRUE];
- (void)dealloc
{
[db close];
[super dealloc];
}
@end