1.什麼是FMDB?
FMDB是iOS平台下SQLite數據庫,只不過它是OC方式封裝了C語言的SQLite語句,使用起來更加面向對象
2.FMDB的優點:1.使用起來更加面向對象; 2.對比蘋果自帶的 Core Data 數據管理工具更加的輕量級,更加的靈活,而且FMDB支持跨平台; 3.提供多線程下的數據安全保護機制,有效地防止數據混亂
3.FMDM中重要的類:
FMDBDataBase: 它代表一個數據庫對象,(我們需要創建數據庫對象時就使用這個類)
FMDBDataBaseQueue: 它提供多線程下執行查找刪除,或者更新的數據安全保護
FMResultSet: 用來存儲sql語句執行結果的集(我們執行完sql語句後得到的結果都在這個類的對象中)
ViewController.m
#import FMDB.h #import Person.h #import DetailViewController.h @interface ViewController () @property(nonatomic,retain)FMDatabase *db; @property(nonatomic,retain)NSMutableArray *dataArray;//存儲查詢到的所有Person對象 @end @implementation ViewController - (void)dealloc { self.db = nil; self.dataArray = nil; [super dealloc]; } - (NSMutableArray *)dataArray{ if (_dataArray == nil) { self.dataArray = [NSMutableArray arrayWithCapacity:0]; } return [[_dataArray retain]autorelease]; }
調用:(介紹FMDB)
- (void)viewDidLoad { [super viewDidLoad]; //獲取Documents文件夾路徑 NSString *urlString = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]; //在Documents文件夾裡創建數據庫文件 db.sqlite NSString *dbPath = [urlString stringByAppendingPathComponent:@db.sqlite]; //創建數據庫對象 //參數:數據庫的路徑 //執行結束後並沒有幫我們生成數據庫文件,只是幫我們創建了數據庫對象 FMDatabase *db = [FMDatabase databaseWithPath:dbPath]; NSLog(@%@,NSHomeDirectory()); //代開數據庫 //open操作才幫我們真正的創建數據庫文件,且如果已經打開,直接返回YES,此時可以直接使用,如果打開失敗會打印報錯信息 BOOL isOpen = [db open]; if (isOpen) { NSLog(@打開成功); //創建表格 //executeUpdate 除了查詢之外,其他數據創建表格,插入數據、刪除數據都是用這個方法 //blob 二進制流 相當於oc的NSData BOOL isCreat = [db executeUpdate:@create table if not exists Person(id integer primary key autoincrement,name text,gender text,age integer,photo blob)]; NSLog(@%@,isCreat ? @建表成功:@建表失敗); }else{ NSLog(@打開失敗); } //給屬性賦值 self.db = db; NSLog(@%@,NSHomeDirectory()); }
插入:
- (IBAction)insert:(UIButton *)sender { Person *p = [[Person alloc]initWithName:@郭美美 gender:@女 age:20 photo:[UIImage imageNamed:@3.gif]]; //將圖片轉化成NSData對象 NSData *data = UIImagePNGRepresentation(p.photo); //@(p.age)參數必須是對象類型的才能使用 //插入操作 BOOL isInsert = [self.db executeUpdate:@insert into Person(name,gender,age,photo)values(?,?,?,?),p.name,p.gender,@(p.age),data]; NSLog(@%@,isInsert ? @插入成功:@插入失敗); }
刪除:
- (IBAction)delete:(UIButton *)sender { //根據條件刪除 BOOL result = [self.db executeUpdate:@delete from Person where id = ?,@3]; NSLog(@%@,result ? @刪除成功:@刪除失敗); //刪除全部表格內容 // BOOL isResult1 = [self.db executeUpdate:@delete from Person]; //刪除表格 // BOOL isResult2 = [self.db executeUpdate:@drop table Person]; }
更新:
- (IBAction)update:(UIButton *)sender { BOOL isUpdate = [self.db executeUpdate:@update Person set gender = ? where id = ?,@男,@4]; NSLog(@%@,isUpdate ? @更新成功:@更新失敗); }
查詢:
- (IBAction)select:(UIButton *)sender { //查詢全部 FMResultSet *set = [self.db executeQuery:@select * from Person]; //按條件查詢 // FMResultSet *set = [self.db executeQuery:@select *from Person where name = ?,@郭美美]; self.dataArray = [NSMutableArray arrayWithCapacity:0]; //循環取出表中的數據 //[set next] 判斷寫一行是否有數據 while ([set next]) { //取出每一個字段對應的數據 NSInteger ID = [set intForColumn:@id];//取出id字段下的數據 NSString *name = [set stringForColumn:@name];//取出name字段下的數據 NSString *gender = [set stringForColumn:@gender];//取出gender字段下的數據 NSInteger age = [set intForColumn:@age];//取出age字段下的數據 NSData *data = [set dataForColumn:@photo];//取出photo字段下的數據 //創建model類 //將二進制流轉成圖片 UIImage *image = [UIImage imageWithData:data]; Person *p = [[Person alloc]initWithName:name gender:gender age:age photo: image]; p.ID = ID; [self.dataArray addObject:p]; [p release]; } }
需要准備一個自定義cell、UIViewController界面和model類
傳值使用:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ //獲取segue起始端的視圖控制器對象 ViewController *rootVC = [segue sourceViewController]; //通過segue完成跳轉的時候會觸發這個方法,在跳轉之前觸發,一般用來傳值 //獲取push過去後的視圖控制器對象 DetailViewController *detailVC = [segue destinationViewController]; //屬性傳值 detailVC.dataSource = rootVC.dataArray; }
准備一個UIViewController:
DetailViewController.h @interface DetailViewController : UITableViewController @property(nonatomic,retain)NSMutableArray *dataSource;//屬性傳值使用 @end DetailViewController.m #import DetailViewController.h #import Person.h #import PersonCell.h #define kPersonCell @personcell @interface DetailViewController () @end @implementation DetailViewController - (void)dealloc { self.dataSource = nil; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return self.dataSource.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:kPersonCell forIndexPath:indexPath]; //建立model類 Person *person = self.dataSource[indexPath.row]; //調用cell賦值的方法 [cell assginValues:person]; return cell; }
布局如下:
PersonCell.h #import@class Person; @interface PersonCell : UITableViewCell @property (retain, nonatomic) IBOutlet UILabel *nameLabel; @property (retain, nonatomic) IBOutlet UILabel *genderLabel; @property (retain, nonatomic) IBOutlet UILabel *ageLabel; @property (retain, nonatomic) IBOutlet UIImageView *photoView; @property (retain, nonatomic) IBOutlet UILabel *IDLabel; //寫一個賦值方式 - (void)assginValues : (Person *)person; @end //================================================= PersonCell.m #import PersonCell.h #import Person.h @implementation PersonCell - (void)awakeFromNib { // Initialization code } //寫一個賦值方式 - (void)assginValues : (Person *)person{ self.nameLabel.text = person.name; self.genderLabel.text = person.gender; self.ageLabel.text = [NSString stringWithFormat:@%ld,person.age]; self.photoView.image = person.photo; self.IDLabel.text = [NSString stringWithFormat:@%ld,person.ID]; } - (void)dealloc { [_nameLabel release]; [_genderLabel release]; [_ageLabel release]; [_photoView release]; [_IDLabel release]; [super dealloc]; } @end
Person.h #import@class UIImage; @interface Person : NSObject @property(nonatomic,copy)NSString *name,*gender; @property(nonatomic)NSInteger age,ID;//基本數據類型可以省略內存修飾符assign,因為默認使用的就是assign @property(nonatomic,retain)UIImage *photo; //自定義初始化方法 - (id)initWithName : (NSString *)name gender : (NSString *)gender age : (NSInteger)age photo : (UIImage *)photo; @end //============================= Person.m #import Person.h #import @implementation Person - (void)dealloc { self.name = nil; self.gender = nil; self.photo = nil; [super dealloc]; } //自定義初始化方法 - (id)initWithName : (NSString *)name gender : (NSString *)gender age : (NSInteger)age photo : (UIImage *)photo{ if (self = [super init]) { self.name = name; self.gender = gender; self.age = age; self.photo = photo; } return self; } - (NSString *)description { return [NSString stringWithFormat:@%@ %@ %ld %@, self.name,self.gender,self.age,self.photo]; } @end