FMDB 增删改查
fmdb相关
导入
#import "FMDB.h"
和
首先,将Collection.sqlite文件,拷贝到docment,文件中
1 // 拷贝文件 2 - (void)copyFile{ 3 4 // 获取文件夹的路径 5 NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 6 7 // 拼接文件名 8 NSString *path = [doc stringByAppendingPathComponent:@"Collection.sqlite"]; 9 10 // 获取文件管理者 11 NSFileManager *fileManager = [NSFileManager defaultManager]; 12 13 // 如果path路径中不存在该文件,就开始拷贝 14 if (![fileManager fileExistsAtPath:path]) { 15 // 获取项目中(bundle)中的sqlite文件 16 NSString *score = [[NSBundle mainBundle] pathForResource:@"Collection" ofType:@"sqlite"]; 17 18 NSError *err; 19 // 文件管理者拷贝文件 srcPath:原文件路径, dstPath:目标路径 20 BOOL fileCopy = [fileManager copyItemAtPath:score toPath:path error:&err]; 21 22 fileCopy ? NSLog(@"数据库 拷贝 OK") : NSLog(@"数据库 拷贝 NO"); 23 }else{ 24 NSLog(@"数据库 已存在"); 25 } 26 }
只有查询返回的为对象,而修改,增加,删除都是返回bool类型值
数据库---查询
// 获取docment文件夹路径 NSString *string = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [string stringByAppendingPathComponent:@"Collection.sqlite"]; // 获取数据库对象 FMDatabase *db = [FMDatabase databaseWithPath:path]; // 数据源临时数组 NSMutableArray *tempArray = [[NSMutableArray alloc] init]; // 打开数据库,返回bool if ([db open]) { // select 执行语句 NSString *query = [NSString stringWithFormat:@"select content,icon_uri,parent_id,conid from love"]; // 执行sql select 语句 返回数据对象 FMResultSet *state = [db executeQuery:query]; NSString *temp = nil; // 将“游标” 向下移动 有下一条数据,就返回true,否则返回0 while ([state next]) { // 模型对象 KKDetailItem *love = [[KKDetailItem alloc] init]; // 获得具体值 love.content = [state stringForColumn:@"content"]; temp =[state stringForColumn:@"icon_uri"]; love.icon_uri = [temp isEqualToString:@"(null)"] ? nil : temp; love.parent_id = [state stringForColumn:@"parent_id"]; love.id = [state stringForColumn:@"conid"]; [tempArray addObject:love]; } } // 关闭数据库 [db close];
数据库---添加
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [doc stringByAppendingPathComponent:@"Collection.sqlite"]; FMDatabase *db = [FMDatabase databaseWithPath:path]; if ([db open]) { NSString *query = [NSString stringWithFormat:@"select loveid from love where parent_id='%@' and conid='%@'",self.item.parent_id,self.item.id]; FMResultSet *state = [db executeQuery:query]; if ([state next]) { [db close]; return; } NSString *insert = [NSString stringWithFormat:@"insert into love('content','icon_uri','conid','parent_id') values('%@','%@','%@','%@')", contentText, icon_uriText, self.item.id, self.item.parent_id]; BOOL ins = [db executeUpdate:insert]; if (ins) { [SVProgressHUD showSuccessWithStatus:@"收藏成功"]; }else{ [SVProgressHUD showSuccessWithStatus:@"收藏出错"]; } } [db close];
数据库---删除
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [doc stringByAppendingPathComponent:@"Collection.sqlite"]; FMDatabase *db = [FMDatabase databaseWithPath:path]; if ([db open]) { NSString *delete = [NSString stringWithFormat:@"delete from love where conid = '%@' and parent_id = '%@'",item.id,item.parent_id]; BOOL de = [db executeUpdate:delete]; if (de) {} } [db close];
数据库---修改
FMDatabase *db = [FMDatabase databaseWithPath:path]; if ([db open]) { NSString *delete = [NSString stringWithFormat:@"delete from student where name = '%@'",self.user.name]; BOOL de = [db executeUpdate:delete]; if (de) {} }