首先今天我們來講講FMDB,他·是一個輕量級的數(shù)據(jù)庫翔试,我個人認為比較好用轻要。
下面展示代碼:
首先拉入FMDB庫。
首先創(chuàng)一個View垦缅,通過單例進行書寫SQL:
.h文件:#import#import "FMDatabase.h"
#import "FMDateFictionModel.h"http://引入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(@"數(shù)據(jù)表創(chuàng)建失敗");
}
}
//添加
-(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(@"數(shù)據(jù)相同");
}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];
}
//創(chuàng)建model文件//添加需要的屬性名
@interface FMDateFictionModel : NSObject
@property(nonatomic,assign)NSUInteger IDs;
@property(nonatomic,strong)NSString *theBookName;//書名
@property(nonatomic,strong)NSString *theBookImage;//書面
@property(nonatomic,strong)NSString *theBookNumber;//章節(jié)
@property(nonatomic,strong)NSString *theBookID;//ID
//引入頭文件
通過model添加調(diào)用方法
修改
//刪除
-(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];
}
//查詢調(diào)用單例方法
theArr = [[FMDateFiction sharedFM]getAll];
簡單的實現(xiàn)就是這么多冲泥。