IOS開發-本地持久化存儲sqlite應用

  1. 前言
  2. 需求描述
  3. 開發測試環境
  4. FMDB介紹
  5. 建立工程

 

 1、前言

     上一章介紹瞭如何開發一個IOS應用的入門案例教程:html

                     個人第一個IOS開發應用 
 
  本章主要將介紹如何使用sqlite,來進行本地持久化存儲數據。
 

2、需求描述

在遊戲開始一段時間後,咱們須要存儲角色的基礎信息,以便我休息以後繼續進行上次的旅途。

 

3、開發環境介紹

OS X EI Captian10.11.4

Xcode 7.3
ios:9.3
機型:iphone 6s/iphone 6s plus

 

4、FMDB介紹

iOS中的數據持久化方式,基本上有如下四種:

1. 屬性列表
2. 對象歸檔
3. SQLite3
4. Core Data

 本文主要介紹如何使用「SQLite3」 持久化方式。ios

SQLite:是一個開源的嵌入式關係數據庫,它在2000年由D. Richard Hipp發佈,它的減小應用程序管理數據的開銷,
SQLite可移植性好,很容易使用,很小,高效並且可靠。
參考地址:http://www.sqlite.org/ FMDB:iOS、macOS開源的第三方庫對SQLite的操做進行了封裝。
參考地址:https://github.com/ccgus/fmdb.git

 

 5、建立工程

Xcode 英文版:git

1.Create a new Xcode projectgithub

 

2.「Choose a template for your new projectiOS > Application > Single View Applicationsql

 

 

3. 「Choose options for your new project」數據庫

Bundle Identifiercn.oshine.ios.Lesson02數組

Language : Objective-C , xcode

Devices: iPhone ,app

Use Core Data: No,iphone

include Unit Tests:  No,

include UI Tests: No

 

 4. "Select Folder To Create"

 

 

下載FMDB,FMDB的目錄結構

 

fmdb.xcodeproj拖動到工做區中。

Lesson02 TARGETS 

Build Pharses:

Target Dependencies > FMDB iOS(fmdb)

Link Binary With Libraries > libsqlite3.0.tbd

Link Binary With Libraries > libFMDB-iOS.a

 

 

 

引入頭文件:

#import <Foundation/Foundation.h>

#import "fmdb/FMDB.h"

#import <sqlite3.h>

 

建立數據庫:

   

FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];

    

if (![db open]) {

         NSLog(@"OPEN FAIL");

        return;

}

 

 

關閉數據庫:

[db close];

 

 

建立表:

 [db executeUpdate:@"CREATE TABLE IF NOT EXISTS profile(name text,val text)"];

 

 

插入記錄:

  [db beginTransaction];

    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"HP",@"600"];

    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"MP",@"250"];

    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"攻擊",@"70"];

    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"防護",@"1"];

    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"攻速",@"0.3"];

    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"移速",@"320"];

    [db commit];

 

  

 

讀取記錄:

   

 FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];

    

    if (![db open]) {

        NSLog(@"OPEN FAIL");

        return;

    }

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

    FMResultSet *rs = [db executeQuery:@"SELECT name,val FROM profile"];

    while ([rs next]) {

        [dictionary setObject:[rs stringForColumn:@"val"] forKey:[rs stringForColumn:@"name"]];

    }

    [rs close];

    [db close];

 

案例界面:

 

案例代碼:

 

 

 

運行結果:

 

ViewController.h

//
//  ViewController.h
//  Lesson02
//
//  Created by ouyangjunqiu on 16/4/7.
//  Copyright © 2016年 oshine. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "fmdb/FMDB.h"
#import <sqlite3.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *label;

- (IBAction)createTable:(id)sender;

- (IBAction)initRole:(id)sender;

- (IBAction)readProfile:(id)sender;

@end

 

ViewController.m

 

//
//  ViewController.m
//  Lesson02
//
//  Created by ouyangjunqiu on 16/4/7.
//  Copyright © 2016年 oshine. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)createTable:(id)sender {
    FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
    
    if (![db open]) {
         NSLog(@"OPEN FAIL");
        return;
    }
    
    [db executeUpdate:@"CREATE TABLE IF NOT EXISTS profile(name text,val text)"];
    [db close];
}

- (IBAction)initRole:(id)sender {
    FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
    
    if (![db open]) {
        NSLog(@"OPEN FAIL");
        return;
    }
    
    [db beginTransaction];
    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"HP",@"600"];
    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"MP",@"250"];
    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"攻擊",@"70"];
    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"防護",@"1"];
    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"攻速",@"0.3"];
    [db executeUpdate:@"INSERT INTO profile(name,val) VALUES (?,?)",@"移速",@"320"];
    [db commit];
    [db close];
    
}

- (IBAction)readProfile:(id)sender{
    FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
    
    if (![db open]) {
        NSLog(@"OPEN FAIL");
        return;
    }
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    FMResultSet *rs = [db executeQuery:@"SELECT name,val FROM profile"];
    while ([rs next]) {
        [dictionary setObject:[rs stringForColumn:@"val"] forKey:[rs stringForColumn:@"name"]];
    }
    [rs close];
    [db close];
    
    [self show:dictionary];
}


-(void)show:(NSMutableDictionary *)dictionary {
    
    self.label.numberOfLines = 0;
    
    NSString * text = [[NSString alloc] init];
    for(NSString *key in dictionary) {
        text = [NSString stringWithFormat:@"%@%@:%@\n",text,key,[dictionary objectForKey:key]];
    }
    self.label.text = text;
}

@end

案例結束

 

NSMutableDictionary Class Reference (key->value可增加數組)

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/

相關文章
相關標籤/搜索