IOS开发-本地持久化存储sqlite应用
2016-04-08 16:00
405 查看
前言
需求描述
开发测试环境
FMDB介绍
创建工程
我的第一个IOS开发应用
本章主要将介绍如何使用sqlite,来进行本地持久化存储数据。
本文主要介绍如何使用“SQLite3” 持久化方式。
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的目录结构
![](https://images2015.cnblogs.com/blog/124819/201604/124819-20160408154904656-262084755.png)
把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
![](https://images2015.cnblogs.com/blog/124819/201604/124819-20160408154954437-435946873.png)
引入头文件:
创建数据库:
关闭数据库:
创建表:
插入记录:
读取记录:
案例界面:
![](https://images2015.cnblogs.com/blog/124819/201604/124819-20160408155233750-1513122050.png)
案例代码:
![](https://images2015.cnblogs.com/blog/124819/201604/124819-20160408155257437-194204917.png)
![](https://images2015.cnblogs.com/blog/124819/201604/124819-20160408155329968-1501186933.png)
![](https://images2015.cnblogs.com/blog/124819/201604/124819-20160408155355437-2083558560.png)
运行结果:
![](https://images2015.cnblogs.com/blog/124819/201604/124819-20160408155417422-1198211705.png)
ViewController.h
ViewController.m
案例结束
NSMutableDictionary Class Reference (key->value可增长数组)
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/
需求描述
开发测试环境
FMDB介绍
创建工程
一、前言
上一章介绍了如何开发一个IOS应用的入门案例教程:我的第一个IOS开发应用
本章主要将介绍如何使用sqlite,来进行本地持久化存储数据。
二、需求描述
在游戏开始一段时间后,我们需要存储角色的基础信息,以便我休息之后继续进行上次的旅途。
三、开发环境介绍
OS X EI Captian:10.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 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的目录结构
![](https://images2015.cnblogs.com/blog/124819/201604/124819-20160408154904656-262084755.png)
把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
![](https://images2015.cnblogs.com/blog/124819/201604/124819-20160408154954437-435946873.png)
引入头文件:
#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];
案例界面:
![](https://images2015.cnblogs.com/blog/124819/201604/124819-20160408155233750-1513122050.png)
案例代码:
![](https://images2015.cnblogs.com/blog/124819/201604/124819-20160408155257437-194204917.png)
![](https://images2015.cnblogs.com/blog/124819/201604/124819-20160408155329968-1501186933.png)
![](https://images2015.cnblogs.com/blog/124819/201604/124819-20160408155355437-2083558560.png)
运行结果:
![](https://images2015.cnblogs.com/blog/124819/201604/124819-20160408155417422-1198211705.png)
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/
相关文章推荐
- sqlite封装库SmartDB1.3发布
- 亲密接触Redis-第一天
- Oracle之物化视图
- Redis使用介绍
- mysql的并发控制
- ORACLE修改对应字符集编码
- mysql中TINYINT的取值范围
- 《MySQL必知必会学习笔记》:存储过程
- 数据库root密码删除
- MYSQL 更改数据库data存储目录 创建用户 创建权限 设置远程访问的权限.
- oracle 数据库 命令
- Redis常用命令速查 <第二篇>
- ORACLE各种小指令
- 找不到mysql服务
- 主机、数据库日志收集
- 15个初学者必看的基础SQL查询语句
- MYSQL之N参数和e参数之命令行执行sql语句
- MySQL之——添加索引的几种方式
- redis主备哨兵---总结
- MYSQL企业常用架构与调优经验分享