<!--StartFragment--> 本文主要介紹如何使用“SQLite3” 持久化方式。 Xcode 英文版: 1.“Create a new Xcode project” 2.“Choose a template for your new project”> iOS > Applicatio ...
- 需求描述
- 開發環境介紹
- FMDB介紹
- 創建工程
- 引入FMDB到工程
- 使用案例代碼
- 運行展示
在游戲開始一段時間後,我們需要存儲角色的基礎信息,以便我休息之後繼續進行上次的旅途。
OS X EI Captian:10.11.4
Xcode: 7.3
ios:9.3
機型:iphone 6s/iphone 6s plus
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 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可增長數組)