iOS sqlite 工具類
阿新 • • 發佈:2019-02-04
#import "DatabaseAdapter.h" @implementation DatabaseAdapter - (id) init { if (self = [super init]) { [self createDatabase]; } return self; } static DatabaseAdapter *sharedManager = nil; + (DatabaseAdapter *)shareManager { static dispatch_once_t once; dispatch_once(&once, ^{ sharedManager = [[self alloc]init]; [sharedManager createDatabase]; }); return sharedManager; } //建立資料庫 - (void) createDatabase { NSString * writableDBPath = [self applicationDocumentsDirecrotyFile]; const char* cpath = [writableDBPath UTF8String]; if (sqlite3_open(cpath, &db) != SQLITE_OK) { sqlite3_close(db); NSAssert(NO, @"資料庫開啟失敗"); } else { char *err; NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS note(_id integer primary key autoincrement, title text, content text, createDate text, updateDate text)"]; const char* cSql = [sql UTF8String]; if (sqlite3_exec(db, cSql, NULL, NULL, &err) != SQLITE_OK) { sqlite3_close(db); NSAssert(NO, @"建表失敗"); } sqlite3_close(db); } } //新增資料 - (void) create:(Note *)note { NSString *path = [self applicationDocumentsDirecrotyFile]; const char* cpath = [path UTF8String]; if (sqlite3_open(cpath, &db) != SQLITE_OK) { sqlite3_close(db); NSAssert(NO, @"資料庫開啟失敗"); } else { NSString *sql = @"insert into note(title, content, createDate, updateDate)values(?,?,?,?)"; const char* cSql = [sql UTF8String]; sqlite3_stmt *statement; if (sqlite3_prepare_v2(db, cSql, -1, &statement, NULL) == SQLITE_OK) { const char* cTitle = [note.title UTF8String]; const char* cContent = [note.content UTF8String]; const char* cCreateDate = [note.createDate UTF8String]; const char* cUpdateDate = [note.updateDate UTF8String]; //繫結引數 sqlite3_bind_text(statement, 1, cTitle, -1, NULL); sqlite3_bind_text(statement, 2, cContent, -1, NULL); sqlite3_bind_text(statement, 3, cCreateDate, -1, NULL); sqlite3_bind_text(statement, 4, cUpdateDate, -1, NULL); //執行插入 if (sqlite3_step(statement) != SQLITE_DONE) { NSAssert(NO, @"插入資料失敗"); } } sqlite3_finalize(statement); sqlite3_close(db); } } //刪除資料 - (void) remove:(int) _id { NSString *path = [self applicationDocumentsDirecrotyFile]; const char* cpath = [path UTF8String]; if (sqlite3_open(cpath, &db) != SQLITE_OK) { sqlite3_close(db); NSAssert(NO, @"資料庫開啟失敗"); } else { NSString *sql = @"delete from note where _id = ?"; const char* cSql = [sql UTF8String]; sqlite3_stmt *statement; if (sqlite3_prepare_v2(db, cSql, -1, &statement, NULL) == SQLITE_OK) { sqlite3_bind_int(statement, 1, _id); if (sqlite3_step(statement) != SQLITE_DONE) { NSAssert(NO, @"插入資料失敗"); } } sqlite3_finalize(statement); sqlite3_close(db); } } //更新資料 - (void) update:(Note *) note { NSString *path = [self applicationDocumentsDirecrotyFile]; const char* cpath = [path UTF8String]; if (sqlite3_open(cpath, &db) != SQLITE_OK) { sqlite3_close(db); NSAssert(NO, @"資料庫開啟失敗"); } else { NSString *sql = @"update note set title = ?, content = ?, updateDate = ? where _id = ?"; const char* cSql = [sql UTF8String]; sqlite3_stmt *statement; if (sqlite3_prepare_v2(db, cSql, -1, &statement, NULL) == SQLITE_OK) { const char* cTitle = [note.title UTF8String]; const char* cContent = [note.content UTF8String]; const char* cUpdateDate = [note.updateDate UTF8String]; sqlite3_bind_text(statement, 1, cTitle, -1, NULL); sqlite3_bind_text(statement, 2, cContent, -1, NULL); sqlite3_bind_text(statement, 3, cUpdateDate, -1, NULL); sqlite3_bind_int(statement, 4, note._id); if (sqlite3_step(statement) != SQLITE_DONE) { NSAssert(NO, @"插入資料失敗"); } } sqlite3_finalize(statement); sqlite3_close(db); } } //按id查詢資料 - (Note *) findById:(int) _id { NSString *path = [self applicationDocumentsDirecrotyFile]; const char* cpath = [path UTF8String]; if (sqlite3_open(cpath, &db) != SQLITE_OK) { sqlite3_close(db); NSAssert(NO, @"資料庫開啟失敗"); } else { NSString *sql = @"select _id, title, content, createDate, updateDate from note where _id = ?"; const char* cSql = [sql UTF8String]; sqlite3_stmt *statement; if (sqlite3_prepare_v2(db, cSql, -1, &statement, NULL) == SQLITE_OK) { sqlite3_bind_int(statement, 1, _id); //執行 if(sqlite3_step(statement) == SQLITE_ROW) { //id int cId = sqlite3_column_int(statement, 0); //標題 char* cTitle = (char*)sqlite3_column_text(statement, 1); NSString *title = [[NSString alloc] initWithUTF8String:cTitle]; //內容 char* cContent = (char*)sqlite3_column_text(statement, 2); NSString *content = [[NSString alloc] initWithUTF8String:cContent]; //建立時間 char* cCreateDate = (char*)sqlite3_column_text(statement, 3); NSString *createDate = [[NSString alloc] initWithUTF8String:cCreateDate]; //更新時間 char* cUpdateDate = (char*)sqlite3_column_text(statement, 4); NSString *updateDate = [[NSString alloc] initWithUTF8String:cUpdateDate]; Note *note = [[Note alloc] initWithID:cId title:title content:content createDate:createDate updateDate:updateDate]; sqlite3_finalize(statement); sqlite3_close(db); return note; } } sqlite3_finalize(statement); sqlite3_close(db); } return nil; } //查詢所有資料 - (NSMutableArray *) findAll { NSMutableArray *listData = [[NSMutableArray alloc] init]; NSString *path = [self applicationDocumentsDirecrotyFile]; const char* cpath = [path UTF8String]; if (sqlite3_open(cpath, &db) != SQLITE_OK) { sqlite3_close(db); NSAssert(NO, @"資料庫開啟失敗"); } else { NSString *sql = @"select _id, title, content, createDate, updateDate from note"; const char* cSql = [sql UTF8String]; sqlite3_stmt *statement; if (sqlite3_prepare_v2(db, cSql, -1, &statement, NULL) == SQLITE_OK) { //執行 while(sqlite3_step(statement) == SQLITE_ROW) { //id int cId = sqlite3_column_int(statement, 0); //標題 char* cTitle = (char*)sqlite3_column_text(statement, 1); NSString *title = [[NSString alloc] initWithUTF8String:cTitle]; //內容 char* cContent = (char*)sqlite3_column_text(statement, 2); NSString *content = [[NSString alloc] initWithUTF8String:cContent]; //建立時間 char* cCreateDate = (char*)sqlite3_column_text(statement, 3); NSString *createDate = [[NSString alloc] initWithUTF8String:cCreateDate]; //更新時間 char* cUpdateDate = (char*)sqlite3_column_text(statement, 4); NSString *updateDate = [[NSString alloc] initWithUTF8String:cUpdateDate]; Note *note = [[Note alloc] initWithID:cId title:title content:content createDate:createDate updateDate:updateDate]; [listData addObject:note]; } } sqlite3_finalize(statement); sqlite3_close(db); } return listData; } //分頁查詢 //limit:查詢條數 //ship:跳過條數 - (NSMutableArray *) findLimit:(int) limit withSkip:(int) skip { NSMutableArray *listData = [[NSMutableArray alloc] init]; NSString *path = [self applicationDocumentsDirecrotyFile]; const char* cpath = [path UTF8String]; if (sqlite3_open(cpath, &db) != SQLITE_OK) { sqlite3_close(db); NSAssert(NO, @"資料庫開啟失敗"); } else { NSString *sql = @"select _id, title, content, createDate, updateDate from note limit ? offset ?"; const char* cSql = [sql UTF8String]; sqlite3_stmt *statement; if (sqlite3_prepare_v2(db, cSql, -1, &statement, NULL) == SQLITE_OK) { sqlite3_bind_int(statement, 1, limit); sqlite3_bind_int(statement, 2, skip); //執行 while(sqlite3_step(statement) == SQLITE_ROW) { //id int cId = sqlite3_column_int(statement, 0); //標題 char* cTitle = (char*)sqlite3_column_text(statement, 1); NSString *title = [[NSString alloc] initWithUTF8String:cTitle]; //內容 char* cContent = (char*)sqlite3_column_text(statement, 2); NSString *content = [[NSString alloc] initWithUTF8String:cContent]; //建立時間 char* cCreateDate = (char*)sqlite3_column_text(statement, 3); NSString *createDate = [[NSString alloc] initWithUTF8String:cCreateDate]; //更新時間 char* cUpdateDate = (char*)sqlite3_column_text(statement, 4); NSString *updateDate = [[NSString alloc] initWithUTF8String:cUpdateDate]; Note *note = [[Note alloc] initWithID:cId title:title content:content createDate:createDate updateDate:updateDate]; [listData addObject:note]; } } sqlite3_finalize(statement); sqlite3_close(db); } return listData; } //獲取資料庫路徑 - (NSString *) applicationDocumentsDirecrotyFile { NSArray * documentDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * myDocPath = [documentDirectory objectAtIndex:0]; NSString * writableFile = [myDocPath stringByAppendingPathComponent:@"note.db"]; return writableFile; } @end