使用SQLite3持久儲存應用程式資料,sqlite3應用程式
前言
SQL是一種資料庫查詢語言,用於存取資料以及查詢、更新和管理關聯性資料庫系統,因為強大的查詢功能和簡單的文法,已經成為主流資料庫的標準語言。SQLite3是一種嵌入式的資料庫,無需伺服器支援,它將SQL語句嵌入到一般通用程式設計語言程式中去,SQL語句負責對資料庫中資料的提取及操作,它所提取的資料將逐行提交給程式,程式中其他語句負責資料的處理。Sqlite3的程式介面是基於C語言的,它在儲存和檢索大量資料上非常有效,而且能夠彙總複雜的資料,更快地處理資料擷取結果。與使用對象處理資料相比,最大的優點就是不必把所有的對象載入到記憶體中,而只是提取符合特定條件的對象。
在項目中使用SQLite3的開發流程
1.設計生產資料庫
第一步、下載安裝SQLite Manager工具:首先開啟Firefox瀏覽器,在工具下面選擇“附加組件”,在瀏覽器右上方的搜尋方塊中輸入“SQLite”,然後選擇並安裝SQLite Manager。
第二步、安裝完成後會提示是否重新啟動瀏覽器,重新啟動後,再次點擊工具會看到SQLite Manager已經出現在選項中。開啟它可以看到資料庫工具介面,到這裡我們就完成了下載安裝SQLite Manager工具的操作。
第三步、建立SQLite3資料庫:首先,在SQLite Manager中,選擇空白建立表徵圖,建立一個資料庫,輸入定義的資料庫名NoteSQL.sqlite。
第四步、然後,點擊Create Table表徵圖,建立一個新的表並命名為NoteSQL。編輯表中的內容。最後就完成了資料庫的建立工作。
2.建立項目並把資料庫檔案匯入到項目中
第一步、將剛才建立的資料庫加到你的項目工程目錄下。
第二步、點擊工程名-》TARGETS-》Build Phases,選擇Link Binary With Libraires,加入libsqlite3.tbd檔案。
3.用資料庫寫入和讀取資料
第一步、進行資料庫的複製操作,將我們加入工程的資料庫,複製一份到我們的應用程式的Documents檔案目錄下
//資料庫的複製操作- (void)createDatabaseIfNeeded:(NSString *)filename{ BOOL success; NSFileManager * fileManager = [NSFileManager defaultManager]; NSError * error; NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [paths objectAtIndex:0]; NSString * writableDBPath = [documentsDirectory stringByAppendingPathComponent:filename]; NSLog(@"%@",writableDBPath); success = [fileManager fileExistsAtPath:writableDBPath]; if (success) { return; } NSString * defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filename]; success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; if (!success) { NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); }}
第二步、開啟資料庫
//開啟資料庫- (void)openDB{ NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * documenthPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"NoteSQL.sqlite"]; int returnValue = sqlite3_open([documenthPath UTF8String], &database); if (returnValue != SQLITE_OK) { sqlite3_close(database); NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database)); }}
第三步、開啟資料庫插入資料
if (sqlite3_open([[self dataFilePath] UTF8String], &_database) != SQLITE_OK) { sqlite3_close(_database); NSAssert(0, @"Failed to open database"); } //插入操作 NSString * insertSQL = [[NSString alloc] initWithFormat:@"insert into NoteSQL(title, date, types, content) values('%@','%@','%@','%@')",titleStr, dateStr, typesStr, contentStr]; char * errorMsg2; if (sqlite3_exec(_database, [insertSQL UTF8String], NULL, NULL, &errorMsg2) != SQLITE_OK) { NSAssert1(0, @"Error updating tables:%s", errorMsg2); sqlite3_free(errorMsg2); }
sqlite3_close(_database);
第四步、開啟資料庫並從中讀取資料
if (sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK) { sqlite3_close(database); NSAssert(0, @"Failed to open database"); } NSString * query = @"select * from NoteSQL order by date"; sqlite3_stmt * statement; if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) { while (sqlite3_step(statement) == SQLITE_ROW) { char * titleChar = (char *)sqlite3_column_text(statement, 0); char * dateChar = (char *)sqlite3_column_text(statement, 1); char * typesChar = (char *)sqlite3_column_text(statement, 2); char * contentChar = (char *)sqlite3_column_text(statement, 3); //C 字串轉換成NSString NSString * title = [[NSString alloc] initWithUTF8String:titleChar]; NSString * date = [[NSString alloc] initWithUTF8String:dateChar]; NSString * types = [[NSString alloc] initWithUTF8String:typesChar]; NSString * content = [[NSString alloc] initWithUTF8String:contentChar]; recordInfo * record = [[recordInfo alloc] init]; record.title = [title copy]; record.types = [types copy]; record.date = [date copy]; record.content = [content copy]; [mutArray addObject:record]; } sqlite3_finalize(statement); }