标签:
FMDB 用到了sqlite3 中的一些语法 这里有sqlite的一些借鉴语法
http://www.runoob.com/sqlite/sqlite-data-types.html
(使用前准备工作,通过cocoapod 或者在github上down一份 FMDB第三方的库 https://github.com/ccgus/fmdb,
然后:添加库)
使用步骤:
1.创建数据库(如果没有数据库的情况)
2.打开数据库(每次操作前都必须保证数据库是打开状态)
3.创建一张保存数据的表(用于保存需要保存类的数据)
4.进行 增 删 改 查(模糊查)等操作!(操作前必须保证,数据库是打开状态。操作结束数据库关闭)
下面写一个例子:
// // Person.h // FMDB // // Created by admin on 16/1/19. // Copyright © 2016年 123. All rights reserved. // #import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomic,assign)int schoolID; @property (nonatomic,retain)NSString *name; @property (nonatomic,retain)NSData *imageData; +(Person *)personWithName:(NSString *)name schoolID:(int)schoolID imagedata:(NSData *)data; @end
// // Person.m // FMDB // // Created by admin on 16/1/19. // Copyright © 2016年 123. All rights reserved. // #import "Person.h" @interface Person() @end @implementation Person +(Person *)personWithName:(NSString *)name schoolID:(int)schoolID imagedata:(NSData *)data { Person *per = [[Person alloc]init]; per.name = name; per.schoolID = schoolID; per.imageData = data; return per; } @end
以上是需要 保存一个 Person类 有name(NSString) schoolID(int) imageData(NSDate)三种数据类型
// // SaveData.h // FMDB // // Created by admin on 16/1/19. // Copyright © 2016年 123. All rights reserved. // #import <Foundation/Foundation.h> #import "FMDB.h" #import "Person.h" @interface SaveData : NSObject //通过这个方法可以获取一个单例的对象 +(SaveData *)shareObj; -(void)createTable; -(NSString *)getSavePath; /****** 增 ******/ -(BOOL)insertPerson:(Person *)aPerson; /****** 删 ******/ -(BOOL)deletePerson:(NSString *)personName; /****** 改 ******/ -(BOOL)updatePerson:(Person *)aPerson; /****** 查 ******/ -(NSArray *)queryPerson; @end
// // SaveData.m // FMDB // // Created by admin on 16/1/19. // Copyright © 2016年 123. All rights reserved. // #import "SaveData.h" @interface SaveData () @property(nonatomic,strong)FMDatabase *fmdb; @end @implementation SaveData //通过这个方法可以获取一个单例的对象 +(SaveData *)shareObj { //声明一个静态的 Singel 对象 static SaveData *sharedInstance = nil; static dispatch_once_t onceToken; //保证block里的代码只执行一次(进行原子操作) dispatch_once(&onceToken, ^{ sharedInstance = [[SaveData alloc] init]; }); return sharedInstance; } #pragma mark - 获取保存路径 -(NSString *)getSavePath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [paths firstObject]; NSString *dbPath = [path stringByAppendingPathComponent:@"fileName.db"]; return dbPath; } #pragma mark - 获取数据库 -(void)getFMDBDataBase { //判断数据库是否存在 if (!self.fmdb) { //如果不存在就创建一个数据库 self.fmdb = [FMDatabase databaseWithPath:[self getSavePath]]; } } #pragma mark - 获取存储表 -(void)createTable { if (![self operationBeforeSaveData]) return; //创建文件管理对象 NSFileManager *fm = [NSFileManager defaultManager]; //如果存储表不可执行 if (![fm isExecutableFileAtPath:[self getSavePath]]) { //http://www.runoob.com/sqlite/sqlite-data-types.html // //创建以张表 BOOL isSuccess = [self.fmdb executeUpdate:@"create table if not exists testTable(ID INTEGER PRIMARY KEY AUTOINCREMENT, schoolID int,perName text,imageData BLOB)"]; /* 仅作提示 */ if (isSuccess) NSLog(@"创建表成功"); else NSLog(@"创建表失败"); } else { NSLog(@"**** 此时表已经存在无需创建 ****"); } } #pragma mark - 在操作数据库前的 需要处理的事情 -(BOOL)operationBeforeSaveData { //获取数据库,在它内部,实现了如果不存在自动创建 [self getFMDBDataBase]; //开启数据库 if (![self.fmdb open]) { NSLog(@"开启数据库失败"); return NO; } //为数据库设置缓存,提高查询效率 [self.fmdb setShouldCacheStatements:YES]; return YES; } #pragma mark - 增 -(BOOL)insertPerson:(Person *)aPerson { if (![self operationBeforeSaveData]) return NO; /* 向表中加入数据 1.先在表中查找有没有相同的元素在表中 2.如果有 则对其进行修改 3.如果没有 则向表中插入一条 */ FMResultSet *set = [self.fmdb executeQuery:@"select * from testTable where perName = ?",aPerson.name]; if ([set next])//表格中找到了与插入数据相同的元素--通过唯一标识name来判断 { //更新数据 [self.fmdb executeUpdate:@"update testTable set perName = ?,imageData = ? where schoolID = ?",aPerson.name,aPerson.imageData,[NSString stringWithFormat:@"%d",aPerson.schoolID]]; [self.fmdb close]; //如果SQL语句执行成功 return YES; } else//在表中没有找到与将要插入数据相同的元素 { [self.fmdb executeUpdate:@"insert into testTable (perName,schoolID,imageData) values (?,?,?)",aPerson.name,[NSNumber numberWithInt:aPerson.schoolID],aPerson.imageData]; [self.fmdb close]; //SQL 语句执行失败 return YES; } } #pragma mark - 删 -(BOOL)deletePerson:(NSString *)personName { if (![self operationBeforeSaveData]) return NO; /* 1.删除操作首先找要删除的数据是否在表中 2.如果在表中则执行删除操作 3.如果不在表中则无需操作直接关闭数据库返回 */ FMResultSet *set = [self.fmdb executeQuery:@"select * from testTable where perName = ?",personName]; if ([set next])//数据库中找到了 { //删除数据 [self.fmdb executeUpdate:@"delete from testTable where perName = ?",personName]; } //关闭数据库 [self.fmdb close]; return YES; } #pragma mark - 改 -(BOOL)updatePerson:(Person *)aPerson { if (![self operationBeforeSaveData]) return NO; /* 修改数据 1.查找需要修改的数据是否存在 2.如果不存在则 关闭数据库 返回不做其他处理 3.如果存在则用新数据替换掉旧数据 */ FMResultSet *set = [self.fmdb executeQuery:@"select * from testTable where perName = ?",aPerson.name]; if ([set next])//数据库中找到了 { [self.fmdb executeUpdate:@"update testTable set schoolID = ?,imageData = ? where perName = ? ",[NSString stringWithFormat:@"%d",aPerson.schoolID],aPerson.imageData,aPerson.name]; } [self.fmdb close]; return YES; } #pragma mark - 查 -(NSArray *)queryPerson { if (![self operationBeforeSaveData]) return nil; NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0]; FMResultSet *set = [self.fmdb executeQuery:@"select * from testTable"]; while ([set next]) { Person *per = [[Person alloc]init]; per.name = [set stringForColumn:@"perName"]; per.schoolID = [set intForColumn:@"schoolID"]; per.imageData = [set dataForColumn:@"imageData"]; [arr addObject:per]; } [self.fmdb close]; return arr; } #pragma mark - 模糊查找数据 -(NSArray *)queryByUnclearName:(NSString *)name { if (![self operationBeforeSaveData]) return nil; NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0]; NSString *nameLike = [NSString stringWithFormat:@"%@%%",name]; FMResultSet *set = [self.fmdb executeQuery:@"select * from testTable where perName like ?",nameLike]; while ([set next]) { Person *per = [[Person alloc]init]; per.name = [set stringForColumn:@"perName"]; per.schoolID = [set intForColumn:@"schoolID"]; per.imageData = [set dataForColumn:@"imageData"]; [arr addObject:per]; } [self.fmdb close]; return arr; } @end
以上:实现对person类 类操作数据的所有操作类
测试调用:
// // ViewController.m // FMDB // // Created by admin on 16/1/18. // Copyright © 2016年 123. All rights reserved. // #import "ViewController.h" #import "Person.h" #import "SaveData.h" @interface ViewController () @property(nonatomic,strong)FMDatabase *db; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString *path = [[NSBundle mainBundle]pathForResource:@"3" ofType:@"png"]; NSData *data = [NSData dataWithContentsOfFile:path]; [[SaveData shareObj]createTable]; for (int i = 0; i < 10; i++) { //添加 Person *per = [Person personWithName:[NSString stringWithFormat:@"person%d--name",i]schoolID:1000+i imagedata:data]; [[SaveData shareObj]insertPerson:per]; } //查询 NSArray *arr = [[SaveData shareObj]queryPerson]; for (Person *item in arr) NSLog(@" 1111111 -- name:%@ -- schoolID:%d -- ",item.name,item.schoolID); NSLog(@"\n\n\n\n"); //删除 [[SaveData shareObj]deletePerson:@"person0--name"]; //再次查询 NSArray *arr1 = [[SaveData shareObj]queryPerson]; for (Person *item in arr1) NSLog(@" 222222 -- name:%@ -- schoolID:%d -- ",item.name,item.schoolID); NSLog(@"\n\n\n\n"); //修改 Person *per = [Person personWithName:@"person1--name" schoolID:1234 imagedata:data]; [[SaveData shareObj]updatePerson:per]; //再次查询 NSArray *arr2 = [[SaveData shareObj]queryPerson]; for (Person *item in arr2) NSLog(@" 33333 -- name: %@ -- schoolID: %d --",item.name,item.schoolID); } @end
测试结果:
文章到这借结束啦,希望对路过的朋友有所帮助!!!!
标签:
原文地址:http://www.cnblogs.com/Mgs1991/p/5143837.html