标签:
一.FMDB 的框架引入点击此处去GitHub下载
二.FMDB 的优缺点
优点:使用起来更加面向对象,省去了很多麻烦、冗余的C语言代码;对比苹果自带的Core Data框架,更加轻量级和灵活;提供了多线程安全的数据库操作方法,有效地防止数据混乱。
三.FMDB 中三个基础类:
1.FMDatabase一个FMDatabase对象就代表一个单独的SQLite数据库用来执行SQL语句;
2.FMResultSet使用FMDatabase执行查询后的结果集;
3.FMDatabaseQueue用于在多线程中执行多个查询或更新,它是线程安全的;
四.用一个实际列子学习FMDB的简单使用,包括数据库的建立、表的建立、数据的插入、数据的查询,其中数据插入,删除、更改、建表操作方式相同。
1..h文件的内容
// // ViewController.h // FMDB // // Created by linan on 15/3/30. // Copyright (c) 2015年 ln. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *userName; @property (weak, nonatomic) IBOutlet UITextField *passWord; - (IBAction)sureButton:(id)sender; - (IBAction)registButton:(id)sender; @end
2..m文件
// // ViewController.m // FMDB // // Created by linan on 15/3/30. // Copyright (c) 2015年 ln. All rights reserved. // #import "ViewController.h" #import "FMDB.h" @interface ViewController () @property(nonatomic,strong)FMDatabase *db; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents/"]; NSString *fmdbPath = [path stringByAppendingString:@"student.sqlite"]; NSLog(@"path = %@",path); self.db = [FMDatabase databaseWithPath:fmdbPath]; if (![self.db open]) { NSLog(@"数据库没有打开"); }else{ NSLog(@"数据库已经打开"); //creat table BOOL result = [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT ,name text NOT NULL ,age integer NOT NULL,password text NOT NULL);"]; if (result) { NSLog(@"creat student is success!"); //successfully create and insert date // [self insertDate]; }else{ NSLog(@"creat student is fail!"); } } } -(void)insertDate{ for (int i = 0; i < 20; i++) { NSString *student_name = [NSString stringWithFormat:@"fool-%d",arc4random()%40]; NSString *student_passWord = [NSString stringWithFormat:@"123456"]; int student_age = arc4random()%100; //未知类型用占位符表示 [self.db executeUpdate:@"INSERT INTO t_student (name,password,age) VALUES(?,?,?);",student_name,student_passWord,@(student_age)]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //search date from sqlite - (IBAction)sureButton:(id)sender { //execute result FMResultSet *result = [self.db executeQuery:@"SELECT *FROM t_student"]; while ([result next]) { NSString *name = [result stringForColumn:@"name"]; NSString *password = [result stringForColumn:@"password"]; if ([self.userName.text isEqualToString:name]&&[self.passWord.text isEqualToString:password]) { [self showAlert:@"登陆成功"]; break; }else{ // } } } - (IBAction)registButton:(id)sender { NSString *student_name = @"zhuce"; int age = 20; NSString *student_passWord = @"123456"; if ([self.db open]) { [self.db executeUpdate:@"INSERT INTO t_student (name,password,age) VALUES (?,?,?);",student_name,student_passWord,@(age)]; } } -(void)showAlert:(NSString*)message{ UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示:" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } @end
简单说明:寻找沙盒中得路径->建立数据库->(open)创建t_students并设置四个字段用来存储学生信息->插入数据。
做一个简单的页面登陆,从数据库中查找学生信息,如有则可以登陆,否则不能登陆可进行注册账号(数据插入操作)
图一是数据库中得数据,图二为storyBoard中拖拉控件。
标签:
原文地址:http://www.cnblogs.com/li--nan/p/4379245.html