IOS开发指南读书笔记11(IOS数据持久层的建立3)

IOS开发指南读书笔记11(IOS数据持久层的建立3)
基于SQLite的数据持久的实现

建立SQLite数据管理

//sqlite数据管理

@interface SQLiteNoteDao : NSObject<BaseNoteDao>
{
 
sqlite3* _db;
 
}

#define DBName @"Notes.sqlite3"
 
@end 

为Note添加一个方法

-(id)initWithSqliteStmt:(sqlite3_stmt *)statement

{

    self = [super init];

    if (self) {

        //第二个参数为字段编号从0开始

        char* cID = (char*)sqlite3_column_text(statement, 0);

        self.ID = [[[NSString alloc]initWithUTF8String:cID] integerValue];

        

        char* cContent = (char*)sqlite3_column_text(statement, 1);

        self.content = [[NSString alloc]initWithUTF8String:cContent];

    }

    return self;

}


具体实现

@implementation SQLiteNoteDao


+(id)sharedManager

{

    static dispatch_once_t onceToken;

    __block SQLiteNoteDao* instance = nil;

    dispatch_once(&onceToken, ^{

        instance = [[SQLiteNoteDao alloc]init];

        [instance createObjectCacheFile];

    });

    return instance;

}


-(void)createObjectCacheFile

{

    

    if ([self openDB]) {

        char* err;

        NSString* createTableSQL = @"CREATE TABLE IF NOT EXISTS Note(ID INTEGER PRIMARY KEY,content TEXT)";

        if (sqlite3_exec(_db, [createTableSQL UTF8String], NULLNULL, &err)!= SQLITE_OK) {

            sqlite3_close(_db);

            NSAssert1(NO@"建表失败,%s",err);

        }

        sqlite3_close(_db);

    }

}


-(NSString *)ObjectCacheFilePath

{

    NSString* documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYESlastObject];

    return [documentDirectory stringByAppendingPathComponent:DBName];

}


-(BOOL)openDB

{

    NSString* dbPath = [self ObjectCacheFilePath];

    if (sqlite3_open([dbPath UTF8String], &_db)!= SQLITE_OK) {

        sqlite3_close(_db);

        NSAssert(NO@"数据库打开失败");

        return NO;

    }

    return YES;

}


-(void)addNote:(Note *)note

{

    if ([self openDB]) {

        NSString* exeSql = @"INSERT INTO Note(ID,content) VALUES(?,?)";

        sqlite3_stmt *statement;

        if (sqlite3_prepare_v2(_db, [exeSql UTF8String], -1, &statement, NULL) == SQLITE_OK) {

            

            sqlite3_bind_text(statement, 1, [[NSString stringWithFormat:@"%d",note.ID]UTF8String], -1nil);

             sqlite3_bind_text(statement, 2, [note.content UTF8String], -1nil);

            

            if (sqlite3_step(statement) != SQLITE_DONE) {

                NSAssert(NO@"数据库插入失败");

            }

        }

        sqlite3_finalize(statement);

        sqlite3_close(_db);

    }

}


-(void)removeNote:(Note *)note

{

    if ([self openDB]) {

        NSString* exeSql = @"DELETE FRON Note where ID=?";

        sqlite3_stmt *statement;

        if (sqlite3_prepare_v2(_db, [exeSql UTF8String], -1, &statement, NULL) == SQLITE_OK) {

            

            sqlite3_bind_text(statement, 1, [[NSString stringWithFormat:@"%d",note.ID]UTF8String], -1nil);

            

            if (sqlite3_step(statement) != SQLITE_DONE) {

                NSAssert(NO@"数据库插入失败");

            }

        }

        sqlite3_finalize(statement);

        sqlite3_close(_db);

    }

}


-(void)modify:(Note *)note

{

    if ([self openDB]) {

        NSString* exeSql = @"UPDATE Note set content=? and ID=?";

        sqlite3_stmt *statement;

        if (sqlite3_prepare_v2(_db, [exeSql UTF8String], -1, &statement, NULL) == SQLITE_OK) {

            

            sqlite3_bind_text(statement, 2, [[NSString stringWithFormat:@"%d",note.ID]UTF8String], -1nil);

            sqlite3_bind_text(statement, 1, [note.content UTF8String], -1nil);

            

            if (sqlite3_step(statement) != SQLITE_DONE) {

                NSAssert(NO@"数据库插入失败");

            }

        }

        sqlite3_finalize(statement);

        sqlite3_close(_db);

    }

}


-(NSMutableArray*)queryAllNote

{

    NSMutableArray* datas = [NSMutableArray array];

    if ([self openDB]) {

        NSString* exeSql = @"SELECT * FROM Note";

        sqlite3_stmt *statement;

        if (sqlite3_prepare_v2(_db, [exeSql UTF8String], -1, &statement, NULL) == SQLITE_OK) {

            

            while (sqlite3_step(statement) == SQLITE_ROW) {

                Note* note = [[Note alloc]initWithSqliteStmt:statement];

                [datas addObject:note];

            }

        }

        sqlite3_finalize(statement);

        sqlite3_close(_db);

    }

    return datas;

}


-(Note*)queryNoteByID:(NSInteger)ID

{

    if ([self openDB]) {

        NSString* exeSql = [NSString stringWithFormat:@"SELECT * FROM Note where ID=%d",ID];

        sqlite3_stmt *statement;

        if (sqlite3_prepare_v2(_db, [exeSql UTF8String], -1, &statement, NULL)) {

            //第二个参数为字段编号从1开始

            sqlite3_bind_text(statement, 1, [[NSString stringWithFormat:@"%d",ID]UTF8String], -1NULL);

            

            if (sqlite3_step(statement) == SQLITE_ROW) {

                Note* note = [[Note alloc]initWithSqliteStmt:statement];

                sqlite3_finalize(statement);

                sqlite3_close(_db);

                return note;

            }

        }

        sqlite3_finalize(statement);

        sqlite3_close(_db);

    }

    return nil;

}



@end 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值