IOS开发-本地持久化存储sqlite应用

  1. 前言
  2. 需求描述
  3. 开发测试环境
  4. FMDB介绍
  5. 创建工程

 

 一、前言

     上一章介绍了如何开发一个IOS应用的入门案例教程:

                    我的第一个IOS开发应用 
 
  本章主要将介绍如何使用sqlite,来进行本地持久化存储数据。
 

二、需求描述

在游戏开始一段时间后,我们需要存储角色的基础信息,以便我休息之后继续进行上次的旅途。

 

三、开发环境介绍

OS X EI Captian10.11.4

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

 

四、FMDB介绍

iOS中的数据持久化方式,基本上有以下四种:

1. 属性列表
2. 对象归档
3. SQLite3
4. Core Data

 本文主要介绍如何使用“SQLite3” 持久化方式。

SQLite:是一个开源的嵌入式关系数据库,它在2000年由D. Richard Hipp发布,它的减少应用程序管理数据的开销,
SQLite可移植性好,很容易使用,很小,高效而且可靠。
参考地址:http://www.sqlite.org/ FMDB:iOS、macOS开源的第三方库对SQLite的操作进行了封装。
参考地址:https://github.com/ccgus/fmdb.git

 

 五、创建工程

Xcode 英文版:

1.Create a new Xcode project

 

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

 

 

3. “Choose options for your new project”

Bundle Identifiercn.oshine.ios.Lesson02

Language : Objective-C ,

Devices: iPhone ,

Use Core Data: No,

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/

posted @ 2016-04-08 16:00  oShine.Q  阅读(875)  评论(0编辑  收藏  举报