你的位置:首页 > 操作系统

[操作系统]IOS开发-本地持久化存储sqlite应用


  1. 需求描述
  2. 开发环境介绍
  3. FMDB介绍
  4. 创建工程
  5. 引入FMDB到工程
  6. 使用案例代码
  7. 运行展示

 

 

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

 

OS X EI Captian:10.11.4

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

 

iOS中的数据持久化方式,基本上有以下四种:1. 属性列表2. 对象归档3. SQLite34. 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 project”>  iOS > Application > Single View Application

 

 

3. “Choose options for your new project”

Bundle Identifier:cn.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/tvos/documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/