FMDB总结
首先今天我们来讲讲FMDB,他·是一个轻量级的数据库,我个人认为比较好用。
下面展示代码:
首先拉入FMDB库。
首先创一个View,通过单例进行书写SQL:
.h文件:#import#import "FMDatabase.h"
#import "FMDateFictionModel.h"//引入model
@interface FMDateFiction : NSObject
+(FMDateFiction *)sharedFM;
-(void)insertMusic:(FMDateFictionModel *)music;
-(NSMutableArray *)getAll;
-(void)deleteMusic:(FMDateFictionModel *)music;
-(void)updataMusic:(FMDateFictionModel *)music;
@end
.m文件:
#import "FMDateFiction.h"
#import "FMResultSet.h"
#import "FMDateFictionModel.h"
static FMDateFiction *fmc = nil;
static FMDatabase *fmdb;
@implementation FMDateFiction
+(FMDateFiction *)sharedFM
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
fmc = [[FMDateFiction alloc]init];
[fmc initDB];
});
return fmc;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
if (!fmc) {
fmc = [super allocWithZone:zone];
}
return fmc;
}
-(id)mutableCopy
{
return self;
}
-(id)copy
{
return self;
}
-(void)initDB
{
NSString *Dotoment = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path = [Dotoment stringByAppendingPathComponent:@"FicTionBooks.sqlite"];
NSLog(@"%@",path);
fmdb = [[FMDatabase alloc]initWithPath:path];
if ([fmdb open]) {
[fmdb executeUpdate:@"CREATE TABLE IF NOT EXISTS musics(ids INTEGER PRIMARY KEY AUTOINCREMENT, theBookName TEXT,theBookImage TEXT,theBookNumber TEXT,theBookID TEXT)"];
[fmdb close];
}else
{
NSLog(@"数据表创建失败");
}
}
//添加
-(void)insertMusic:(FMDateFictionModel *)music
{
BOOL isc = false;
NSArray *arr = [self getAll];
for (FMDateFictionModel *m in arr) {
if ([m.theBookID isEqualToString:music.theBookID]) {
isc = true;
break;
}
}
if (isc) {
NSLog(@"数据相同");
}else{
[fmdb open];
BOOL isb = [fmdb executeUpdate:@"insert into musics values(null,?,?,?,?)",music.theBookName,music.theBookImage,music.theBookNumber,music.theBookID];
if (isb) {
NSLog(@"添加成功");
}
else{
NSLog(@"添加失败");
}
[fmdb close];
}
}
//查询
-(NSMutableArray *)getAll
{
NSMutableArray *arr = [[NSMutableArray alloc]init];
[fmdb open];
FMResultSet *fmset = [[FMResultSet alloc]init];
fmset = [fmdb executeQuery:@"select * from musics"];
while ([fmset next]) {
FMDateFictionModel*m = [[FMDateFictionModel alloc]init];
m.IDs = [fmset intForColumn:@"ids"];
m.theBookName = [fmset stringForColumn:@"theBookName"];
m.theBookImage = [fmset stringForColumn:@"theBookImage"];
m.theBookNumber = [fmset stringForColumn:@"theBookNumber"];
m.theBookID = [fmset stringForColumn:@"theBookID"];
[arr addObject:m];
}
[fmdb close];
return arr;
}
//删除
-(void)deleteMusic:(FMDateFictionModel *)music
{
[fmdb open];
NSString *sql = [NSString stringWithFormat:@"delete from musics where ids = %ld",music.IDs];
BOOL isb = [fmdb executeUpdate:sql];
if (isb) {
NSLog(@"删除成功");
}
else
{
NSLog(@"删除失败");
}
[fmdb close];
}
//修改
-(void)updataMusic:(FMDateFictionModel *)music
{
[fmdb open];
NSString *str = [NSString stringWithFormat:@"update musics set theBookName = '%@' , theBookImage = '%@', theBookNumber = '%@', theBookID = '%@'' where ids = %ld",music.theBookName,music.theBookImage,music.theBookNumber,music.theBookID,music.IDs];
BOOL isb = [fmdb executeUpdate:str];
if (isb) {
NSLog(@"修改成功");
}
else
{
NSLog(@"修改失败");
}
[fmdb close];
}
//创建model文件//添加需要的属性名
@interface FMDateFictionModel : NSObject
@property(nonatomic,assign)NSUInteger IDs;
@property(nonatomic,strong)NSString *theBookName;//书名
@property(nonatomic,strong)NSString *theBookImage;//书面
@property(nonatomic,strong)NSString *theBookNumber;//章节
@property(nonatomic,strong)NSString *theBookID;//ID
//引入头文件
通过model添加调用方法
修改
//删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
FMDateFictionModel *m = [theArr objectAtIndex:indexPath.row];
[[FMDateFiction sharedFM]deleteMusic:m];
theArr = [[FMDateFiction sharedFM]getAll];
[theBookOneView.theBookTable reloadData];
}
//查询调用单例方法
theArr = [[FMDateFiction sharedFM]getAll];
简单的实现就是这么多。