iOS开发中的4种数据持久化方式(2) :数据库 SQLite3、Core Data 的运用
在上文,我们介绍了iOS开发中的其中2种数据持久化方式:属性列表、归档解档。
本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3、Core Data 的运用;
在本节,将通过对4个文本框内容的创建、修改,退出后台,再重新回到后台,来认识这两种持久化数据的方式。效果图如下【图1】:
【本次开发环境: Xcode:7.2 iOS Simulator:iphone6 By:啊左】
(本节2个项目demo的下载:数据库SQLite3的运用Demo、Core Data 的运用Demo)
一、数据库SQLite3
SQLite(Strutcured Query Language,结构化查询语言),是iOS的嵌入式SQL数据库,在存储和检索大量数据方面非常有效,属于轻量级数据库,但是功能很强大。 安卓和ios开发使用的都是SQLite数据库。 而另一种持久化数据方式,core Data是对SQLite的封装,因为iOS中使用的SQLite是纯C语言的。
1、链接到SQLite3库
在Xcode中,使用Single View Application模板创建一个新项目,命名为persistence3。
新建项目选中项目导航列表(最左边)的顶部然后在主区域的TARGETS部分选中persistence3,注意要从TARGETS选中而不是从PROJECT部分。
选中后,点击“Build Phases”,打开在第三栏,按“+”添加“libsqlite3.0.tbd”【注意:Xcode7后dylib后缀改成tbd,如果仍要添加.bylib为后缀的链接,在添加framework那个对话框,最下面有个 “add other…” 点开之后, 按command+shift+G , 路径输入 /usr/lib/ ,然后 找到你需要的lib文件 就ok了。。 好吧,我还是习惯添加.dyib…】
3是版本号,是SQLite的第三个版本。它是始终指向最新版本的SQLite3库的;
在“Main.storyboard”中拖入4个标签、4个文本框控件,拖动并对齐标签与文本框,并依次修改标签文本如【图1】,“ViewController.h”中添加一个装载4个文本框的数组“lineFields”:
1 2 3 4 5 |
#import @interface ViewController : UIViewController @property (nonatomic,strong)IBOutletCollection(UITextField) NSArray *lineFields; //存储4个文本框字段的数组 @end |
然后打开辅助编辑器,通过control键将4个文本框连接到 lineFields 这个数组,确保连接顺序为从顶部到底部!
在项目导航面板中,点击”ViewController.m” ,将以下2段代码添加到@implementation与 @end 的中间,与上文相同,这个方法在后面会一直调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#import "ViewController.h" #import //导入SQLite3,注意是扩折号 //SQLite 是不区分大小写的 @implementation ViewController{ sqlite3 *sqlite; //数据库 } //懒加载 -(NSString *)datafilePath{ NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [array objectAtIndex:0]; return [path stringByAppendingPathComponent:@"data.sqlite"]; } |
在这里,我们介绍一下iOS9的一个新的变化:UIAlertController。小小地在这里运用一下:
1 2 3 4 5 6 7 8 9 10 11 12 |
//警告提示框,为后面的操作向用户提示信息 -(void)alert:(NSString *)mes{ /*知识点:ios 9.0 后,简单的UIAlertView已经不能用了。 UIAlertController代替了UIAlertView弹框 和 UIActionSheet下弹框 */ //UIAlertControllerStyleAlert:中间; UIAlertControllerStyleActionSheet:显示在屏幕底部; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:mes preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil]; UIAlertAction *defult = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil]; [alert addAction:cancel]; [alert addAction:defult]; [self presentViewController:alert animated:YES completion:nil]; //呈现 } |
- (void)viewDidLoad 以及通知的方法代码:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849501 - (void)viewDidLoad {2 [super viewDidLoad];3 int result = sqlite3_open([[self datafilePath]UTF8String], &sqlite)pan>[[self datafilePath]UTF8String], &sqlite)列表、归档解档。本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3、Core Data 的运用;
在本节,将通过对4个文本框内容的创建、修改,退出后台,再重新回到后台,来认识这两种持久化数据的方式。效果图如下【图1】:
【本次开发环境: Xcode:7.2 iOS Simulator:iphone6 By:啊左】(本节2个项目demo的下载:数据库SQLite3的运用Demo、Core Data 的运用Demo)
一、数据库SQLite3
SQLite(Strutcured Query Language,结构化查询语言),是iOS的嵌入式SQL数据库,在存储和检索大量数据方面非常有效,属于轻量级数据库,但是功能很强大。 安卓和ios开发使用的都是SQLite数据库。 而另一种持久化数据方式,core Data是对SQLite的封装,因为iOS中使用的SQLite是纯C语言的。
1、链接到SQLite3库
在Xcode中,使用Single View Application模板创建一个新项目,命名为persistence3。
新建项目选中项目导航列表(最左边)的顶部然后在主区域的TARGETS部分选中persistence3,注意要从TARGETS选中而不是从PROJECT部分。
选中后,点击“Build Phases”,打开在第三栏,按“+”添加“libsqlite3.0.tbd”【注意:Xcode7后dylib后缀改成tbd,如果仍要添加.bylib为后缀的链接,在添加framework那个对话框,最下面有个 “add other…” 点开之后, 按command+shift+G , 路径输入 /usr/lib/ ,然后 找到你需要的lib文件 就ok了。。 好吧,我还是习惯添加.dyib…】
3是版本号,是SQLite的第三个版本。它是始终指向最新版本的SQLite3库的;
在“Main.storyboard”中拖入4个标签、4个文本框控件,拖动并对齐标签与文本框,并依次修改标签文本如【图1】,“ViewController.h”中添加一个装载4个文本框的数组“lineFields”:
12345#import@interface ViewController : UIViewController@property (nonatomic,strong)IBOutletCollection(UITextField)NSArray *lineFields; //存储4个文本框字段的数组@end然后打开辅助编辑器,通过control键将4个文本框连接到 lineFields 这个数组,确保连接顺序为从顶部到底部!
在项目导航面板中,点击”ViewController.m” ,将以下2段代码添加到@implementation与 @end 的中间,与上文相同,这个方法在后面会一直调用:
12345678910111213#import "ViewController.h"#import //导入SQLite3,注意是扩折号//SQLite 是不区分大小写的@implementation ViewController{sqlite3 *sqlite; //数据库}//懒加载-(NSString *)datafilePath{NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *path = [array objectAtIndex:0];return [path stringByAppendingPathComponent:@"data.sqlite"];}在这里,我们介绍一下iOS9的一个新的变化:UIAlertController。小小地在这里运用一下:
123456789101112//警告提示框,为后面的操作向用户提示信息-(void)alert:(NSString *)mes{/*知识点:ios 9.0 后,简单的UIAlertView已经不能用了。 UIAlertController代替了UIAlertView弹框 和 UIActionSheet下弹框 *///UIAlertControllerStyleAlert:中间; UIAlertControllerStyleActionSheet:显示在屏幕底部;UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:mes preferredStyle:(UIAlertControllerStyleAlert)];UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];UIAlertAction *defult = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];[alert addAction:cancel];[alert addAction:defult];[self presentViewController:alert animated:YES completion:nil];//呈现}- (void)viewDidLoad 以及通知的方法代码:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849501 - (void)viewDidLoad {2 [super viewDidLoad];3 int result = sqlite3_open([[self datafilePath]UTF8String], &sqlite)s="crayon-num" data-line="crayon-5812944496270037851600-13">13#import "ViewController.h"#import //导入SQLite3,注意是扩折号//SQLite 是不区分大小写的@implementation ViewController{sqlite3 *sqlite; //数据库}//懒加载-(NSString *)
- (void)viewDidLoad 以及通知的方法代码: