iOS開發-數據篇-sqlite存儲

簡介:iOS開發的持久化數據存儲有4種方式:NSUserDefault.plistNSKeyedArchiverNSKeyedUnarchiversqlite3數據庫、CoreDatasql

sqlite3CoreData適合存儲大量數據(內容數據等),而另外兩種適合存儲簡單數據(語言,字體,標記等簡單數據存儲)數據庫

準備:這裏主要介紹利用sqlite3封裝的FMDatabase的數據庫,(固然還有PlausibleDatabasesqlitepersistentobjects等其餘封裝的庫),導入須要依賴的libsqlite3.dylib,FMDatabase已經根據工程不一樣狀況兼容ARC和非ARC。爲了只針對數據庫進行講解,直接建立一個簡單工程,導入FMDatabase庫。
多線程

內容:1.建立數據庫字體

#import <UIKit/UIKit.h>
#import "FMDatabase.h"
#import "FMDatabaseQueue.h"

@interface ViewController : UIViewController{
    FMDatabase *db;
    FMDatabaseQueue *queue_db;
}
//建立數據庫文件路徑
- (NSString*)getPath{
    NSArray* paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) ;
    return [[paths objectAtIndex:0]stringByAppendingPathComponent:@"TEST.sqlite"] ;
}

2.數據庫的建表-增-刪-改操做spa

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self executeSql:[self createTable]];//創表
    [self executeSql:[self insertData]];//
    [self executeSql:[self deleteData]];//
    [self executeSql:[self setData]];////
    [self queryData];
    //開闢多線程
    [NSThread detachNewThreadSelector:@selector(multipleThread) toTarget:self withObject:nil];
    
}
//建立數據庫
-(NSString *)createTable{
    NSString *createSql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS TEST(a interger,b text)"];
    return createSql;
}

//
- (NSString *)insertData{
    NSString *insertSql= [NSString stringWithFormat:
                          @"INSERT INTO '%@' ('%@', '%@') VALUES ('%@', '%@')",
                          @"TEST", @"a", @"b", [NSNumber numberWithInt:1], @"寧波"];
    return insertSql;
}

//
- (NSString *)deleteData{
    NSString *deleteSql = [NSString stringWithFormat: @"DELETE FROM '%@' where %@ = '%@'",
                           @"TEST", @"b", @"寧波"];
    return deleteSql;
}

//
-(NSString *)setData{
    NSString *setSql = [NSString stringWithFormat:@"UPDATE '%@' SET '%@' = '%@' WHERE '%@' = '%@'",
                        @"TEST", @"b",  @"象山" ,@"a",  [NSNumber numberWithInt:1]];
    return setSql;
}

3.數據庫的執行操做線程

//數據庫執行操做
-(BOOL)executeSql:(NSString *)sql{
    db = [FMDatabase databaseWithPath:[self getPath]];
    BOOL success;
    if ([db open]) {
        success  = [db executeUpdate:sql];
    }else{
        NSLog(@"OPEN FAIL");
    }
    [db close];
    return success;
}

4.數據庫的查詢操做code

//
-(void)queryData{
    if ([db open]) {
        NSString *querySql = [NSString stringWithFormat:
                              @"SELECT * FROM %@",@"TEST"];
        FMResultSet * rs = [db executeQuery:querySql];
        while ([rs next]) {
            int a = [rs intForColumn:@"a"];
            NSString *b = [rs stringForColumn:@"b"];
            NSLog(@"a:%d  == b:%@",a,b);
        }
        
        [db close];
    }
}

5.數據庫的多線程操做orm

若在多線程的環境下,不能在多線程中共用同一個FMDatabase進行數據訪問,會形成數據混亂,多線程下不能用單例對象,這時得依靠FMDatabaseQueue。sqlite

//多線程下使用FMDatabaseQueue進行數據庫操做
-(void)multipleThread{
    queue_db = [FMDatabaseQueue databaseQueueWithPath:[self getPath]];
    @autoreleasepool{
        [queue_db inDatabase:^(FMDatabase *db2) {
            [db2 executeUpdate:[self insertData]];
            [self queryData];
        }];
    }
}
相關文章
相關標籤/搜索