DB-class. h file
# Import <Foundation/Foundation. h>
# Import <sqlite3.h>
@ Interface DB: NSObject
+ (Sqlite3 *) openDB; // open the database
-(Void) closeDB; // closes the database
@ End
DB-class. m file
# Import "DB. h"
# Import <sqlite3.h>
Static sqlite3 * db = nil;
@ Implementation DB
+ (Sqlite3 *) openDB
{
If (db)
{
Return db;
}
// Target path
NSString * docPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDirectory, YES) objectAtIndex: 0];
// Original path
NSString * filePath = [docPath stringByAppendingPathComponent: @ "db. sqlite"];
NSFileManager * fm = [NSFileManager defamanager manager];
If ([fm fileExistsAtPath: filePath] = NO) // if NO database exists in the doc, copy it from the bundle.
{
NSString * bundle = [[NSBundle mainBundle] pathForResource: @ "classDB" ofType: @ "sqlite"];
NSError * err = nil;
If ([fm copyItemAtPath: bundle toPath: filePath error: & err] = NO) // if the copy fails
{
NSLog (@ "% @", [err localizedDescription]);
}
}
Sqlite3_open ([filePath UTF8String], & db );
Return db;
}
-(Void) closeDB
{
If (db)
{
Sqlite3_close (db );
}
}
@ End
Person class. h file
# Import <Foundation/Foundation. h>
@ Interface Person: NSObject
@ Property (nonatomic, retain) NSString * name, * phone;
@ Property (nonatomic, assign) int age, ID;
-(Id) initWithName :( NSString *) name phone :( NSString *) phone age :( int) age ID :( int) ID;
+ (NSMutableArray *) findAll;
+ (Int) count;
+ (Person *) findByID :( int) ID;
+ (NSMutableArray *) findByname :( NSString *) name;
+ (Void) addName :( NSString *) name phone :( NSString *) phone age :( int) age;
+ (Void) deleteByID :( int) ID;
+ (Void) updataName :( NSString *) name phone :( NSString *) phone age :( int) age forID :( int) ID;
@ End
Person class. m file
# Import "Person. h"
# Import "DB. h"
@ Implementation Person
@ Synthesize name, ID, phone, age;
-(Id) initWithName :( NSString *) aName phone :( NSString *) aPhone age :( int) aAge ID :( int) aID
{
[Super init];
If (self)
{
Self. name = aName;
Self. phone = aPhone;
Self. age = aAge;
Self. ID = aID;
}
Return self;
}
-(NSString *) description
{
Return [NSString stringWithFormat: @ "id = % d name = % @ phone = % @ age = % d", self. ID, self. name, self. phone, self. age];
}
+ (NSMutableArray *) findAll
{
Sqlite3 * db = [DB openDB];
Sqlite3_stmt * stmt = nil; // create a declaration object
Int result = sqlite3_prepare_v2 (db, "select * from classDB order by ID",-1, & stmt, nil );
NSMutableArray * persons = nil;
If (result = SQLITE_ OK)
{
Persons = [[NSMutableArray alloc] init];
While (sqlite3_step (stmt) = SQLITE_ROW)
{
Int ID = sqlite3_column_int (stmt, 0 );
Const unsigned char * name = sqlite3_column_text (stmt, 1 );
Const unsigned char * phone = sqlite3_column_text (stmt, 2 );
Int age = sqlite3_column_int (stmt, 3 );
Person * p = [[Person alloc] initWithName: [NSString stringwithuf8string :( const char *) name] phone: [NSString stringwithuf8string :( const char *) phone] age: age ID: ID];
[Persons addObject: p];
[P release];
}
}
Else
{
Persons = [[NSMutableArray alloc] init];
}
Sqlite3_finalize (stmt );
Return [persons autorelease];
}
+ (Int) count
{
Sqlite3 * db = [DB openDB];
Sqlite3_stmt * stmt = nil;
Int result = sqlite3_prepare_v2 (db, "select count (ID) from classDB",-1, & stmt, nil );
If (result = SQLITE_ OK)
{
Int count = 0;
If (sqlite3_step (stmt ))
{
Count = sqlite3_column_int (stmt, 0 );
}
Sqlite3_finalize (stmt );
Return count;
}
Else
{
Sqlite3_finalize (stmt );
Return 0;
}
}
+ (Person *) findByID :( int) ID
{
Sqlite3 * db = [DB openDB];
Sqlite3_stmt * stmt = nil;
Person * p = nil;
Int result = sqlite3_prepare_v2 (db, "select * from classDB where ID =? ",-1, & stmt, nil );
If (result = SQLITE_ OK)
{
Sqlite3_bind_int (stmt, 1, ID );
If (sqlite3_step (stmt ))
{
Int ID = sqlite3_column_int (stmt, 0 );
Const unsigned char * name = sqlite3_column_text (stmt, 1 );
Const unsigned char * phone = sqlite3_column_text (stmt, 2 );
Int age = sqlite3_column_int (stmt, 3 );
P = [[Person alloc] initWithName: [NSString stringwithuf8string :( const char *) name] phone: [NSString stringwithuf8string :( const char *) phone] age: age ID: ID];
}
}
Sqlite3_finalize (stmt );
Return [p autorelease];
}
+ (NSMutableArray *) findByname :( NSString *) name
{
Sqlite3 * db = [DB openDB];
Sqlite3_stmt * stmt = nil;
Int result = sqlite3_prepare (db, "select * from classDB where name =? ",-1, & stmt, nil );
NSMutableArray * persons = nil;
If (result = SQLITE_ OK)
{
Sqlite3_bind_text (stmt, 1, [name UTF8String],-1, nil );
Persons = [[NSMutableArray alloc] init];
While (sqlite3_step (stmt) = SQLITE_ROW)
{
Int ID = sqlite3_column_int (stmt, 0 );
Const unsigned char * name = sqlite3_column_text (stmt, 1 );
Const unsigned char * phone = sqlite3_column_text (stmt, 2 );
Int age = sqlite3_column_int (stmt, 3 );
Person * p = [[Person alloc] initWithName: [NSString stringwithuf8string :( const char *) name] phone: [NSString stringwithuf8string :( const char *) phone] age: age ID: ID];
[Persons addObject: p];
[P release];
}
}
Else
{
Persons = [[NSMutableArray alloc] init];
}
Sqlite3_finalize (stmt );
Return [persons autorelease];
}
// Add Element
+ (Void) addName :( NSString *) name phone :( NSString *) phone age :( int) age
{
NSString * str = [NSString stringWithFormat: @ "insert into classDB (name, phone, age) values ('% @', '% @', % d)", name, phone, age];
Sqlite3 * db = [DB openDB];
Sqlite3_stmt * stmt = nil;
Int result = sqlite3_prepare_v2 (db, [str UTF8String],-1, & stmt, nil );
If (result = SQLITE_ OK)
{
Sqlite3_step (stmt );
}
Sqlite3_finalize (stmt );
}
// Delete information by ID
+ (Void) deleteByID :( int) ID
{
NSString * str = [NSString stringWithFormat: @ "delete from classDB where ID = % d", ID];
Sqlite3 * db = [DB openDB];
Sqlite3_stmt * stmt = nil;
Int result = sqlite3_prepare_v2 (db, [str UTF8String],-1, & stmt, nil );
If (result = SQLITE_ OK)
{
Sqlite3_step (stmt );
}
Sqlite3_finalize (stmt );
}
// Update
+ (Void) updataName :( NSString *) name phone :( NSString *) phone age :( int) age forID :( int) ID
{
NSString * str = [NSString stringWithFormat: @ "update classDB set name = '% @', phone = '% @', age = % d where ID = % d", name, phone, age, ID];
Sqlite3 * db = [DB openDB];
Sqlite3_stmt * stmt = nil;
Int result = sqlite3_prepare_v2 (db, [str UTF8String],-1, & stmt, nil );
If (result = SQLITE_ OK)
{
Sqlite3_step (stmt );
}
Sqlite3_finalize (stmt );
}
@ End